Make Unreal REAL.
article thumbnail

 

이 곳에서 C++ 정규 표현식을 온라인으로 테스트해 볼 수 있다.

 

 

Regex - C++ - OneCompiler

#include #include #include using namespace std; int main() { regex reg("Neptune"); if (regex_search("Dark, cold, and whipped by supersonic winds," "ice giant Neptune is the eighth and most distant planet in our" "solar system. More than 30 times as far fro

onecompiler.com

 

string.replace()를 반복 호출해도 되지만 regex_replace() 함수를 이용해 한 번에 교체할 수도 있다.

 

#include <iostream>
#include <regex>

using namespace std;

void main()
{
    string str = "RePLacE this !";
    regex expression("replace", regex_constants::icase);
    
    str = regex_replace(str, expression, "Hello");
    cout << str << endl;
    
    str = "a1W2b3p4o5w";
    expression = regex("[A-z]");
    
    str = regex_replace(str, expression, "");
    cout << str << endl;
    
    str = "a1W2b3p4o5w";
    expression = regex("[0-9]");
    
    str = regex_replace(str, expression, "");
    cout << str << endl;
    
    // 중괄호는 정규 표현식에서 특수한 의미를 가지기 때문에
    // "\\},\\{" 로 해줘야 "\},\{"로 처리된다.
    str = "aaa},{bbb},{ccc";
    expression = regex("\\},\\{");   
    
    str = regex_replace(str, expression, "");
    cout << str;
}

 

출력

Hello this !
12345
aWbpow
aaabbbccc
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그