Skip to content

Commit 4866958

Browse files
committed
Merge pull request #2 from RCmerci/dev
Dev
2 parents 22ea6b6 + 9bb563c commit 4866958

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ GRTAGS
55
GTAGS
66
GPATH
77
a.out
8-
test
8+
source/test/test
99
#*#

readme.md

+118
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,121 @@
55

66

77
于是开坑第三个,名字就叫 regex-eng-3 好了!
8+
9+
========================================
10+
11+
语法:
12+
13+
```
14+
^ -> 匹配开始
15+
16+
$ ->匹配结束
17+
18+
\\d -> [0-9]
19+
20+
\\D -> [^\\d]
21+
22+
\\w -> [a-z0-9_]
23+
24+
\\W -> [^\\w]
25+
26+
\\s -> [ \t\n\r]
27+
28+
\\S -> [^\\s]
29+
30+
. -> [^\n]
31+
32+
| -> 或
33+
34+
[...] -> 字符集
35+
36+
* -> 循环至少0次,贪婪
37+
38+
*? -> 循环至少0次,非贪婪
39+
40+
+ -> 循环至少1次,贪婪
41+
42+
+? -> 循环至少1次,非贪婪
43+
44+
{min, max} -> 循环min至max次,贪婪 (max为空代表无穷)
45+
46+
{min, max}? -> 循环min至max次,非贪婪
47+
48+
49+
(...) -> 普通括号,用于改变优先级
50+
51+
(?...) -> 匿名捕获
52+
53+
(?<name>...) -> 命名捕获
54+
55+
(=...) -> 前向查询
56+
57+
(!...) -> 反前向查询
58+
59+
(#<name>...) -> 表达式命名
60+
61+
(&<name>) -> 表达式使用(即替换成对应表达式)
62+
```
63+
64+
==========================================
65+
66+
API:
67+
```
68+
class RegexResult:
69+
70+
71+
72+
Bool() -> bool
73+
74+
Group() -> 匹配到的字符串数组
75+
76+
Group(index) -> 匿名捕获的字符串数组
77+
78+
Group(name) -> 命名捕获的字符串数组
79+
80+
81+
82+
83+
84+
class Regex:
85+
86+
87+
88+
Match(testString, beginIndex=0) -> RegexResult
89+
90+
Search(testString, beginIndex=0) -> RegexResult
91+
92+
FindAll(testString, beginIndex=0) -> RegexResult
93+
94+
Replace(testString, replaceStr, replaceTime=1, beginIndex=0) -> resultString
95+
96+
ReplaceAll(testString, replaceStr, beginIndex=0) -> resultString
97+
```
98+
99+
100+
============================================
101+
102+
103+
104+
examples:
105+
```
106+
auto rst = Regex("^.+?@(?<type>.+?)\\.(?(com|cn))$").Match("[email protected]");
107+
108+
rst.Group().size() == 1;
109+
110+
rst.Group()[0] == "[email protected]";
111+
112+
rst.Group(1).size() == 1;
113+
114+
rst.Group(1)[0] == "com";
115+
116+
rst.Group("type").size() == 1;
117+
118+
rst.Group("type")[0] == "563748559";
119+
```
120+
121+
122+
more examples in source/test/test.cpp
123+
124+
125+

source/test/test.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -687,17 +687,19 @@ int main()
687687

688688
}
689689

690-
691690
{
692691
std::string testString =
693692
"<dt><a href=\"http://bbs.uestc.edu.cn/forum.php?mod=forumdisplay&fid=219\">出国留学</a><em class=\"xw0 xi1\" title=\"今日\"> (今日: 9)</em></dt>""<dd><a href=\"http://bbs.uestc.edu.cn/forum.php?mod=redirect&amp;tid=1570205&amp;goto=lastpost#lastpost\" class=\"xi2\">ETH网申</a></dd><dd><cite>by: <a href=\"http://bbs.uestc.edu.cn/home.php?mod=space&username=camelluxin\">camelluxin</a> [<span title=\"2015-11-8 21:30\">1&nbsp;小时前</span>]</cite></dd>";
694693
Regex regex("href=\"(?<url>https?://.+?)\"");
695694
auto result = regex.FindAll(testString);
696695
assert(result.Bool() == true);
697-
for (auto i : result.Group("url")) {
696+
697+
}
698+
{
699+
auto rst = Regex("(ab){2,}").FindAll("yyababababababababbab");
700+
for (auto i :rst.Group()) {
698701
std::cout<<i<<std::endl;
699702
}
700-
701703
}
702704
std::cout<<"all test case passed."<<std::endl;
703705
}

0 commit comments

Comments
 (0)