411 REFERENCE · Wikibooks
Regular Expressions/Example
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Examples
Simple Patterns
The simplest and crudest kind of search pattern that can be specified is a simple string, enclosed in /slash/ symbols. For example, the following regular expression searches for any line that contains the /string/ "The". This will not match "the" because regular expressions are /case sensitive/. It will match words like "There" or "Them".
/The/
Symbols in a regular expression
Some /symbol/s can be used within a regular expression to make the search more specific. For example, the /caret/ symbol acts as an /anchor/ that /match/es the beginning of the line. Preceding the regular expression with a /caret/ causes only the /string/ at the beginning of the line to be considered for a /match/. The following regular expression /match/es any line that begins with the string "The". Lines that contain "The" but do not start with it will not be /match/ed.
/^The/
Similarly, the /dollar/ symbol acts as an /anchor/ that /match/es the end of the line. Following the regular expression with a /dollar/ matches any line that ends with search pattern. Lines that do not end with "The" will not be matched in this example.
/The$/
Negation
The following example uses the caret and bracket metacharacters to match any pattern except the word undefined:
/[^(undefined)].*[^(undefined)]/
Character Mapping
The following examples match punctuation characters and replace them with the equivalent XML entity codes:
Ellipsis:
/(…)|(\.\.\.)/…/
Emdash:
/(.)—(.)/$1—$2/
Endash:
/–/–/