References
需要转义的特殊字符:
* . ? + $ ^ [ ] ( ) { } | \ /
Tutorials
Lesson 1 The 123s
\d
any single digit character
\D
any single non-digit character
Lesson 2: The Dot
.
any single character
\.
period
Lesson 3: Matching specific characters
[abc]
only a, b, c single character
Lesson 4: Excluding specific characters
[^abc]
not a, b or c
Lesson 5: Character ranges
[a-z]
characters a to z
[0-9]
number 0 to 9
\w
字母,数字,下划线。等价于[A-Za-z0-9_]
\W
等价于\[^A-Za-z0-9_]
Lesson 6: Catching some zzz’s
{m}
m repetitions
{m, n}
m to n repetitions
Examples:
w{3}
(three w)
[wxy]{5}
(five characters, each of which can be a w, x, or y)
Lesson 7: Mr. Kleene, Mr. Kleene
*
zero or more repetitions *前的字符可以重复0次或者更多次
+
one or more repetitions +前的字符可以重复1次或者更多次
Match aaaabcc
Match aabbbbc
Match aacc
Skip a
answer: aa+b*c+
Lesson 8: Characters optional
?
optional character ?前的字符可以出现0次或者1次
ab?c
will match either the strings “abc
” or “ac
”
Lesson 9: All this whitespace
\s
代替any whitespace 包括space, tab(\t), new line(\n), return(\r)
\S
相反
Lesson 10: Starting and ending
^...$
starts and ends
^Mission: successful$
文本必须以Mission: 开头, successful结尾
Lesson 11: Match groups
(...)
capture group
(a(bc))
capture sub-group
(.*)
capture all
Lesson 14: It’s all conditional
(abc|def)
matches abc or def
Problems
Problem 1: Matching a decimal numbers
Task | Text |
---|---|
Match | 3.14529 |
Match | -255.34 |
Match | 128 |
Match | 1.9e10 |
Match | 123,340.00 |
Skip | 720p |
answer:^-?\d+(,\d+)*(\.\d+(e\d+)?)?$
Problem 2: Matching phone numbers
后面还有很多problems