2017年11月08日09:57:27再次重新系统的开始学习正则表达式,希望这次可以真正的学会!
语法学习
1. Character classes
Character classes match a character from a specific set. There are a number of predefined(预定的) character classes and you can also define your own sets.
用法 |
匹配 |
. |
匹配任何字符,除了换行符(any character except newline) |
w ,d ,s |
字符,数字,空白(word,digit,whitespace) |
W ,D ,S |
非字符,非数字,非空白( not word,whitespace) |
[abc] |
any of a,b,or c 【中间没有任何字符分隔】 |
[^abc] |
not a,b or c (出了个a,c其余的) |
[a-g] |
character between a & g |
b |
匹配单词(word)边界位置,如空格,标点符号或字符串的开始/结尾。这匹配一个位置,而不是一个字符。(Matches a word boundary position such as whitespace,punctuation,or the start/end of the string. This matches a position,not a character.) |
B |
匹配不是单词边界的任意位置,这匹配的是一个位置,不是一个字符。 |
w |
匹配任何单词字符(字母数字和下划线)。只匹配low-ascii字符(没有重音或非罗马字符)。相当于[A-Za-z0-9_](Matches any word character (alphanumeric & underscore). Only matches low-ascii characters (no accented or non-roman characters). Equivalent to [A-Za-z0-9_]) |
W |
Matches any character that is not a word character (alphanumeric & underscore). Equivalent to [^A-Za-z0-9_] |
d |
Matches any digit character (0-9). Equivalent to [0-9]. |
D |
Matches any character that is not a digit character (0-9). Equivalent to [^0-9]. |
s |
Matches any whitespace character (spaces,tabs,line breaks(换行符)). |
S |
Matches any character that is not a whitespace character (spaces,line breaks). |
character set(字符集)[aeiou] |
Match any character in the set. |
negated set(非字符集)[^aeiou] |
Match any character that is not in the set. |
range: [a-z] |
Matches a character having a character code between the two specified characters inclusive.(在两个指定的字符之间匹配具有字符代码的字符) |
2. Anchors (锚)
Anchors are unique in that they match a position within a string,not a character.
是独一无二的,它们匹配的位置在一个字符串,而不是一个字符。
用法 |
匹配 |
begining : ^ |
Matches the beginning of the string,or the beginning of a line if the multiline flag (m) is enabled. This matches a position,not a character. e.g.: /^w+/gm – He is a boy. |
end: $ |
Matches the end of the string,or the end of a line if the multiline flag (m) is enabled. This matches a position,not a character. e.g.:/w+$/g – I am a girl_ |
word boundary(边界)b |
Matches a word boundary position such as whitespace(空白),punctuation(标点符号),or the start/end of the string. This matches a position,not a character. e.g.:/mb/gm – me e**m** ;/bm/gm – **m**e em ; |
not word boundary B |
Matches any position that is not a word boundary. This matches a position,not a character. e.g.:/wB/gm –I **a**m a **girl**_ **m**e **e**m |
3. Escapaed characters(转义字符)
Some characters have special meaning in regular expressions and must be escaped(避免). All escaped characters begin with the character.
Within a character set,only,-,and ] need to be escaped.
用法 |
匹配 |
octal escape:251 |
Octal(八进制) escaped character in the form 00. Value must be less than 255 (377).(八进制需要转义的字符是从
| |