您的位置:首页 > 其它

正则表达式中容易误解的地方

2016-11-13 21:04 281 查看
1. 

\d Any digit
\D Any character except a digit

\s “whitespace”: space, tab, carriage return, line feed, or newline

\S Anything except whitespace

\w A “word character”: [A-Za-z0-9_]

\W Any character except a word character

2.

/[.]/ =~ "b"

֒→ nil
Within brackets, characters like the dot, plus, and star are not special.

3.

/^[\[=\]]+$/ =~ '=]=[='

֒→ 0

To include open and close brackets inside of brackets, escape them with a backslash. This expression matches any sequence of one or more characters, all of which must be either [, ], or =. (The two anchors ensure that there are no characters before or after
the matching characters.)

4. 

caret在[] 中的用法,取反

/[^ab]/ =~ "z"

֒→ 0

Putting a caret at the beginning of a character class causes the set to contain all characters
except the ones listed.

5.

正则表达式中间,竖线的用法:分成两个正则表达式

/^Fine birds | cows ate\.$/ =~

"Fine birds ate seeds."

֒→ 0
A vertical bar divides the regular expression into two smaller regular expressions. A match means that either the entire left regexp matches or the entire right one does.

This regular expression does not mean “Match either 'Fine birds ate.' or 'Fine cows ate.'” It actually matches either a string beginning with "Fine birds" or one ending in "cows ate."
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  正则表达式