[c++] 문자열에서 숫자를 다른 문자열로 치환하는 방법

예를 들어, 문자열에서 모든 숫자를 ‘*‘로 대체하려면 다음과 같이 할 수 있습니다:

#include <iostream>
#include <regex>

int main() {
    std::string input = "abc123def456";
    std::regex pattern("\\d");
    std::string result = std::regex_replace(input, pattern, "*");
    std::cout << result << std::endl;
    return 0;
}

위의 코드에서, std::regex_replace함수를 사용하여 숫자를 ‘‘로 대체했습니다. 정규 표현식 \\d는 숫자를 나타내며, 대체 문자열은 ‘‘입니다.

더 복잡한 패턴을 사용하거나 숫자를 특정한 문자열로 대체하려는 경우에는 정규 표현식을 조정하여 원하는 기능을 얻을 수 있습니다.

더 자세한 내용은 C++의 regex 라이브러리 문서를 참조하시기 바랍니다.

cppreference.com - std::regex_replace cplusplus.com - std::regex_replace