Open main menu

Wikibooks β

Regular Expressions/Example

< Regular Expressions(Redirected from Regular Expressions/example)



Simple PatternsEdit

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".


Symbols in a regular expressionEdit

Some symbols can be used within a regular expression to make the search more specific. For example, the caret symbol acts as an anchor that matches 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 matches any line that begins with the string "The". Lines that contain "The" but do not start with it will not be matched.


Similarly, the dollar symbol acts as an anchor that matches 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 following example uses the caret and bracket metacharacters to match any pattern except the word undefined:


Character MappingEdit

The following examples match punctuation characters and replace them with the equivalent XML entity codes: