Skip to content

Commit fbab74a

Browse files
committedNov 9, 2015
修改gitignore,写readme
1 parent 545b302 commit fbab74a

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-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

+60
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,63 @@
55

66

77
于是开坑第三个,名字就叫 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+

‎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)
Please sign in to comment.