이 곳에서 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
'자료구조 & 알고리즘 > 기타' 카테고리의 다른 글
istream의 기능들 (0) | 2023.03.02 |
---|---|
정답률보다 오답률이 높을 때 (0) | 2023.02.26 |
lower_bound, upper_bound (0) | 2023.02.19 |
multiset, multimap에서 1개 원소만 삭제 (0) | 2023.02.19 |
vector에서 중복 원소 제거 (0) | 2023.02.07 |