regular expressions

egular Expression Operators

a?

matches 0 or 1 occurrence of *a*

'a' or empty string

a*

matches 0 or more occurrences of *a*

empty string or 'a', 'aa', 'aaa', etc

a+

matches 1 or more occurrences of *a*

'a', 'aa', 'aaa', etc

a|b

match *a* or *b*

'a' or 'b'

.

match any single character

'a', 'q', 'l', '_', '+', etc

[woeirjsd]

match any of the named characters

'w', 'o', 'e', 'i', 'r', 'j', 's', 'd'

[1-9]

match any of the characters in the range

'1', '2', '3', '4', '5', '6', '7', '8', '9'

[^13579]

match any characters not named

even digits, or any other character

(ie)

group an expression (for use with other operators)

'ie'

^a

match an *a* at the beginning of a line

'a'

a$

match an *a* at the end of a line

'a'

There are a couple of other things you should know. If you want to use one of the operators above to mean the actual character, like you want to match a question mark, you need to put a '\' in front of it. For example:

// evaluates to true, and will for anything ending in a question mark (that doesn't have a question mark in it) "How tall is Angelina Jolie?" ==~ /[^\?]+\?/

This is your first really ugly regular expression. (The frequent use of these in PERL is one of the reasons it is considered a "write only" language). By the way, google knows how tall she is. The only way to understand expressions like this is to pick it apart:

/

[^?]

+

?

/

begin expression

any character other than '?'

more than one of those

a question mark

end expression

你可能感兴趣的:(regular expressions)