在C++中使用正则表达式可以使用STL中的regex库。示例代码如下:
#include <iostream>
#include <regex>
using namespace std;
int main() {
string str = "Hello, world!";
// 匹配字符串中的"world"
regex reg("world");
if(regex_search(str, reg)) {
cout << "匹配成功!" << endl;
}
// 替换字符串中的","为"@"
string result = regex_replace(str, regex(","), "@");
cout << "替换后的结果为:" << result << endl;
return 0;
}
需要注意的是,regex库需要C++11或以上的支持。另外,正则表达式的语法可以参考regep语法规则。