Regular Expression Components

Regular expressions are useful because they provide more flexible and powerful pattern matching than you can achieve with wildcards.

Basic Elements

Basic element

Example

.

Matches any single character. For example, cou.h matches couth, couch, and cough.

\ followed by a single character

Lets special characters be used as a single character or “escaped”. For example, to use this character: . as a period, precede it with a backslash: \Escaped characters are especially useful when describing paths. For example \.html$ matches any string ending in .html. The following characters must be preceded by a backslash if they are to be used without special meaning: \ . $ * ? + [ ] ( ) |

$

Matches any string where the specified pattern occurs at the end of the string. For example, cause$ matches cause and because but not causes.

^

Matches any string where the specified pattern occurs at the beginning of the string. For example, ^couch matches couches and couch but not uncouch. Use this element carefully when specifying a domain. For example, the expression ^/couch matches /couch/index.htm, but not www.domain.com/couch/index.htm.

[ ]

Matches any single character in the range or set enclosed in the brackets. For example, [aeiou] matches any vowel. You can use a shorthand notation for a range of characters. For example, [0-9] matches any decimal digit.

If the sequence is preceded by a carat:

^

it matches any single character not from the range or set. For example, [^a-z] matches any character that is not a letter of the alphabet.

|

Indicates an OR operator. For example: couch|chair finds couch or chair.

a regular expression in parenthesis ()

Used for grouping expressions. The expression (couch[0-9])|(bed[0-9]) matches couch36A or full_bed33b, but not couch.

a single character

Matches any string containing the single character to be matched. For example, a matches cause. You could also combine several characters together, such as couch.

Qualifying Characters

Any regular expression element can be qualified by one of the following three characters: *, +, or ?.

Qualifying character

Example

*

Matches 0 or more occurrences of the element that precedes it. For example, couch_[a-z0-9]* matches couch_ followed by 0 or more alphanumeric values. This expression matches couch_0, couch_aaa,couch_a33, and couch_. Do not confuse * in a regular expression with * used as a wildcard character. To match all html files in the following path, for example, specify:

/mydir/.*\.html$

not

/mydir/*.html

The correct regular expression above specifies any string containing /mydir/, followed by 0 or more characters, followed by .html, for example /mydir/index.html.

+

Matches 1 or more occurrences of the element it follows. For example, couch_[a-z0-9]+ matches couch_ followed by one or more alphanumeric values. This expression matches couch_0, couch_aaa, and couch_a33, but not couch_.

?

Matches 0 or 1 occurrences of the element it follows. For example, couch_[a-z0-9]? matches couch followed by 0 or 1 alphanumeric values. This expression matches couch_0, couch_, and couch_a, and couch_bb.

Note: Because parameter names and values are not case-sensitive, you do not need to match case in your regular expressions.