February 2018
Beginner to intermediate
348 pages
9h 40m
English
One last feature of the regular expression mechanism is the ability to capture sub-expressions. Whatever text is placed between parentheses ( ) is captured and can be used after the matching process. The captured characters become available under the form of variables called $N, where N is a numeric index, in order of capture. Alternatively, you can attribute an arbitrary name to each of your captures (see the following example). The variables generated through the captures can be inserted within directive values.
Here are a couple of examples to illustrate the principle:
|
Pattern |
Example of a matching string |
Captured |
|
^(hello|hi) (sir|mister)$ |
hello sir |
$1 = hello $2 = sir |
|
^(hello (sir))$ |
hello sir |
$1 = hello ... |
Read now
Unlock full access