基础
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
//regex_match匹配整个字符串
regex reg1("\\w+day");
string s1 = "saturday";
string s2 = "saturday and sunday";
smatch r1;
smatch r2;
cout << boolalpha << regex_match(s1, r1, reg1) << endl;
cout << boolalpha << regex_match(s2, r2, reg1) << endl;
cout << "s1匹配结果:" << r1.str() << endl;
cout << "s2匹配结果:" << r2.str() << endl;
cout << endl;
//regex_match只返回第一个匹配结果
smatch rr1;
smatch rr2;
cout << boolalpha << regex_search(s1, rr1, reg1) << endl;
cout << "s1匹配结果:" << rr1.str() << endl;
cout << boolalpha << regex_search(s2, rr2, reg1) << endl;
cout << "s2匹配结果:" << rr2.str() << endl;
cout << endl;
//使用iterator返回多个匹配结果
//结果要用->
cout << "iterator结果:" << endl;
sregex_iterator it(s2.begin(), s2.end(), reg1);
sregex_iterator end;
for (; it != end; ++it)
{
cout << it->str() << endl;
//cout << *it << endl; 错误
}
cout << "token_iterator结果:" << endl;
sregex_token_iterator tit(s2.begin(), s2.end(), reg1);
sregex_token_iterator tend;
for (; tit != tend; ++tit)
{
cout << tit->str() << endl;
cout << *tit << endl;
}
//子表达式匹配
regex reg2("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})");
string ip = "0:11:222:333";
smatch m;
regex_match(ip, m, reg2);
cout << "输出:str()" << endl;
cout << m.str() << endl;
cout << m.str(1) << endl;
cout << m.str(2) << endl;
cout << m.str(3) << endl;
cout << m.str(4) << endl;
cout << "输出:[i]" << endl;
cout << m[0] << endl;
cout << m[1] << endl;
cout << m[2] << endl;
cout << m[3] << endl;
cout << m[4] << endl;
//输出结果同上
//regex_search(ip, m, str2);
cout << endl;
string ip2 = "0:11:222:333 4:55:66:7";
sregex_iterator ip_it(ip2.begin(), ip2.end(), reg2);
sregex_iterator ip_end;
for (; ip_it != ip_end; ++ip_it)
{
cout << ip_it->str() << endl;
cout << ip_it->str(1) << endl;
cout << ip_it->str(2) << endl;
cout << ip_it->str(3) << endl;
cout << ip_it->str(4) << endl;
}
return 0;
}
参考:https://blog.csdn.net/qq_34802416/article/details/79307102
实战
附上一道可以用到正则的题目以及用正则的解法(Leetcode609:在系统中查找重复文件):https://leetcode.cn/problems/find-duplicate-file-in-system/
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
string root;
int n = 0;
regex reg("(.*)\\((.*)\\)");
smatch r;
vector<string> sp_path;
unordered_map<string, vector<string>> m;
for (string path : paths) {
sp_path = split(path);
root = sp_path[0];
n = sp_path.size();
for (int i = 1; i < n; i++) {
regex_match(sp_path[i], r, reg);
m[r.str(2)].push_back(root + '/' + r.str(1));
}
}
vector<vector<string>> res;
for (auto [key, value] : m) {
if (value.size() > 1) {
res.push_back(value);
}
}
return res;
}
vector<string> split(string str, char sep = ' ') {
vector<string> res;
string temp = "";
for (char ch : str) {
if (ch == sep) {
res.push_back(move(temp));
}
else {
temp += ch;
}
}
res.push_back(move(temp));
return res;
}
};
常用正则表达式
匹配小括号内容
regex reg("(.*)\\((.*)\\)");