匹配字符串的基本规则
1. 匹配固定的字符串
regex e("abc");
2. 匹配固定字符串,不区分大小写
regex e("abc", regex_constants::icase);
3. 匹配固定字符串之外多一个字符,不区分大小写
regex e("abc.", regex_constants::icase); // . Any character except newline. 1个字符
4. 匹配0个或1个字符
regex e("abc?"); // ? Zero or 1 preceding character. 匹配?前一个字符
5. 匹配0个或多个字符
regex e("abc*"); // * Zero or more preceding character. 匹配*前一个字符
6. 匹配1个或多个字符
regex e("abc+"); // + One or more preceding character. 匹配+前一个字符
7. 匹配特定字符串中的字符
regex e("ab[cd]*"); // [...] Any character inside square brackets. 匹配[]内的任意字符
8. 匹配非特定字符串的字符
regex e("ab[^cd]*"); // [...] Any character not inside square brackets. 匹配非[]内的任意字符
9. 匹配特定字符串,且指定数量
regex e("ab[cd]{3}"); // {n} 匹配{}之前任意字符,且字符个数为3个
10. 匹配特定字符串,指定数量范围
regex e("ab[cd]{3,}"); // {n} 匹配{}之前任意字符,且字符个数为3个或3个以上
regex e("ab[cd]{3,5}"); // {n} 匹配{}之前任意字符,且字符个数为3个以上,5个以下闭区间
11. 匹配规则中的某一个规则
regex e("abc|de[fg]"); // | 匹配|两边的任意一个规则
12. 匹配分组
regex e("(abc)de+"); // () ()表示一个子分组
13. 匹配子分组
regex e("(abc)de+\\1"); // () ()表示一个子分组,而\1表示在此位置匹配第一个分组的内容
regex e("(abc)c(de+)\\2\\1"); // \2 表示的是在此匹配第二个分组的内容
14. 匹配某个字符串开头
regex e("^abc."); // ^ begin of the string 查找以abc开头的子字符串
15. 匹配某个字符串结尾
regex e("abc.$"); // $ end of the string 查找以abc结尾的子字符串
以上是最基本的匹配模式的书写。通常如果要匹配特定的字符,需要使用\进行转义,比如在匹配字符串中需要匹配".",那么在匹配字符串中应该在特定字符前加上\。出了以上的基本规则,如果还不满足特定的需要,那么可以参考此链接。使用了解基本的匹配模式后,需要使用正则表达式进行匹配、查找或者替代。