Posted on

A regular expression is a logical formula for operating on strings. It uses predefined special characters and combinations of them to form a “rule string” expressing filtering logic for strings. Quoted from Baidu Baike.
Common regular expressions:
. matches any character (escape .)
[a-z] denotes a character set, any letter from a-z, such as [0-9], [A-Z], and [a-zA-Z0-9], etc.
[^a-z]any character except a lowercase letter
|the pipe means OR, e.g., p(ython|hp)
w denotes a letter, digit, or underscore
d denotes any digit
(   )? means the parenthesized content may appear, but need not, e.g. (http://)?heartofocean..cn matches http://heartofocean.cn and heartofocean.cn
(   )* allows the parenthesized content to appear N times, N≥0
(   )+allows the parenthesized content to appear N times, N≥1
(   ){x,y} allows the parenthesized content to appear N times, x≤N≤y
(?=exp) matches the position before exp
(?<=exp) matches the position after exp
(?!exp) matches a position not followed by exp
(?<!exp) matches a position not preceded by exp
(?#comment) comment
(exp) matches exp and captures the text in an automatically named group
(?<name>exp) matches exp and captures the text in the group named name; it can also be written as(?’name’exp)

Regular Expression HOWTO

d
Matches any decimal digit; this is equivalent to the class [0-9].
D
Matches any non-digit character; this is equivalent to the class [^0-9].
s
Matches any whitespace character; this is equivalent to the class [ tnrfv].
S
Matches any non-whitespace character; this is equivalent to the class [^ tnrfv].
w
Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
W
Matches any non-alphanumeric character; this is equivalent to the class [^a-zA-Z0-9_].

Leave a Reply