|
5 | 5 |
|
6 | 6 |
|
7 | 7 | 于是开坑第三个,名字就叫 regex-eng-3 好了!
|
| 8 | + |
| 9 | +======================================== |
| 10 | + |
| 11 | +语法: |
| 12 | +^ -> 匹配开始 |
| 13 | +$ ->匹配结束 |
| 14 | +\\d -> [0-9] |
| 15 | +\\D -> [^\\d] |
| 16 | +\\w -> [a-z0-9_] |
| 17 | +\\W -> [^\\w] |
| 18 | +\\s -> [ \t\n\r] |
| 19 | +\\S -> [^\\s] |
| 20 | +. -> [^\n] |
| 21 | +| -> 或 |
| 22 | +[...] -> 字符集 |
| 23 | +* -> 循环至少0次,贪婪 |
| 24 | +*? -> 循环至少0次,非贪婪 |
| 25 | ++ -> 循环至少1次,贪婪 |
| 26 | ++? -> 循环至少1次,非贪婪 |
| 27 | +{min, max} -> 循环min至max次,贪婪 (max为空代表无穷) |
| 28 | +{min, max}? -> 循环min至max次,非贪婪 |
| 29 | +(...) -> 普通括号,用于改变优先级 |
| 30 | +(?...) -> 匿名捕获 |
| 31 | +(?<name>...) -> 命名捕获 |
| 32 | +(=...) -> 前向查询 |
| 33 | +(!...) -> 反前向查询 |
| 34 | +(#<name>...) -> 表达式命名 |
| 35 | +(&<name>) -> 表达式使用(即替换成对应表达式) |
| 36 | + |
| 37 | +========================================== |
| 38 | +API: |
| 39 | +class RegexResult: |
| 40 | + |
| 41 | +Bool() -> bool |
| 42 | +Group() -> 匹配到的字符串数组 |
| 43 | +Group(index) -> 匿名捕获的字符串数组 |
| 44 | +Group(name) -> 命名捕获的字符串数组 |
| 45 | + |
| 46 | + |
| 47 | +class Regex: |
| 48 | + |
| 49 | +Match(testString, beginIndex=0) -> RegexResult |
| 50 | +Search(testString, beginIndex=0) -> RegexResult |
| 51 | +FindAll(testString, beginIndex=0) -> RegexResult |
| 52 | +Replace(testString, replaceStr, replaceTime=1, beginIndex=0) -> resultString |
| 53 | +ReplaceAll(testString, replaceStr, beginIndex=0) -> resultString |
| 54 | + |
| 55 | +============================================ |
| 56 | + |
| 57 | +examples: |
| 58 | +auto rst = Regex("^.+?@(?<type>.+?)\\.(?(com|cn))$").Match("563748559@qq.com"); |
| 59 | +rst.Group().size() == 1; |
| 60 | +rst.Group()[0] == "563748559@qq.com"; |
| 61 | +rst.Group(1).size() == 1; |
| 62 | +rst.Group(1)[0] == "com"; |
| 63 | +rst.Group("type").size() == 1; |
| 64 | +rst.Group("type")[0] == "563748559"; |
| 65 | + |
| 66 | +more examples in source/test/test.cpp |
| 67 | + |
0 commit comments