Regular Expressions/Example

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: /(…)|(\.\.\.)/&amp;#8230;/

Emdash: /(.)—(.)/$1&amp;#8212;$2/

Endash: /–/&amp;#8211;/