1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
正则表达式经常被简称为模式 普通字符:纯文本 元字符:在正则表达式里有特殊含义的特殊字符 例如 对于元字符,需要转义系列, 例如\\ \. \[ \] \+ \* \? \s 空格 1 . 匹配任意一个字符(除了换行符),包括"."本身。一个正则表达式允许有多个"." 例如 .apple. log. 如果真需要".",用转义字符 例如 apple\. 2 [] 匹配多个字符的其中一个 例如 [abcd]pple [Aa]pple 匹 配区间 例如 [a-z]pple [0-9]pple [A-Z]pple (要求符合asscii码顺序) [^] 非匹配符,(即取反,不出现此字符) 3 \d 匹配数字,等价[0-9] \D 匹配非数字,等价[^0-9] 4 \w 匹配字母数字下划线,等价[a-zA-Z0-9_] \W 匹配空非下划线 ,等价 [^a-zA-Z0-9_] \S 匹配非空白字符 6 POSIX 例如 [[:alnum:]] [[:alpha:]] [[:digit:]] [[:lower:]] [[:uppper:]] 7 + 匹配一个或多个字符 例如 \w+@\w+\.\w+ 匹配邮箱 字符集合里[],+ . 等可以不使用转移序列,使用也不碍事 * 匹配0个或多个字符 8 ? 匹配0或1个字符 例如 https? 9 {} 匹配重复次数 例如 a{0,4}, 最少出现0次,最多4次 a{3,}, 最少出现3次 a{3} 匹配3次 10 \bcat\b icati \bcat icat cat\b cati \<cat\< dog cat dog 11 (\d{1,3}){1,3} 子表达式 操作符(19|20)\d{2} |
如何在c++中使用
1 2 3 4 |
<span role="presentation"><span class="cm-meta">#include <regex></span><span class="cm-comment">//头文件</span></span> <span role="presentation"><span class="cm-variable">regex</span> <span class="cm-def">s</span>(<span class="cm-string">".apple\.[jh]+"</span>)</span> <span role="presentation"><span class="cm-variable">regex_match</span>(<span class="cm-string">"apple"</span>,<span class="cm-variable">s</span>)<span class="cm-comment">//返回值类型bool</span></span> <span role="presentation"></span> |