|
|
|
|
You can also use other syntax within an RE to control what it matches:
- \(expression\)
- Matches whatever expression matches. You only need to enclose an expression
in these delimiters to use operators (such as * or +) on it and to denote
subexpressions for back referencing (explained later in this section). For EREs,
use the parentheses without the backslashes: (subexpression)
- \n
- Matches the same string that was matched by the nth preceding expression enclosed
in \( \) or, for EREs, ( ). This is called a back reference. n can
be 1 through 9. For example, \(ab\)\1 matches abab, but does not match ac.
If fewer than n subexpressions precede \n, the back reference is not valid.
|
NOTE: You cannot use back references in EREs.
|
- expression*
- Matches zero or more consecutive occurrences of what expression matches. expression
can be a single character or collating symbol, a subexpression, or a back reference
(for BREs). For example, [ab]* matches ab and ababab; b*cd matches characters 3
to 7 of cabbbcdeb.
- expression\{m\}
- Matches exactly m occurrences of what expression matches. expression can
be a single character or collating symbol, a subexpression, or a back reference (for BREs).
For example, c\{3\} matches characters 5 through 7 of ababccccd (the
first 3 c characters only). For EREs, use the braces without the backslashes: {m}
- expression\{m,\}
- Matches at least m occurrences of what expression matches. expression can
be a single character or collating symbol, a subexpression, or a back reference
(for BREs). For example, \(ab\)\{3,\} matches abababab, but does not
match ababac. For EREs, use the braces without the backslashes: {m,}
- expression\{m,u\}
- Matches any number of occurrences, between m and u inclusive, of what expression
matches. expression can be a single character or collating symbol, a
subexpression, or a back reference (for BREs). For example, bc\{1,3\} matches
characters 2 through 4 of abccd and characters 3 through 6 of abbcccccd
For EREs, use the braces without the backslashes: {m,u}
- ^expression
- Matches only sequences that match expression that start at the first character
of a string or after a new-line character if the rexOpenNewline flag was specified.
For example, ^ab matches ab in the string abcdef, but does not
match it in the string cdefab. The expression can be the entire RE or any
subexpression of it.
- expression$
- Matches only sequences that match expression that end the string or that precede
the new-line character if the rexOpenNewline flag was specified. For example,
ab$ matches ab in the string cdefab but does not match it
in the string abcdef. The expression must be the entire RE.
- ^expression$
- Matches only an entire string, or an entire line if the rexOpenNewline flag was
specified. For example, ^abcde$ matches only abcde.
In addition to those listed above, you can also use the following specifiers for EREs
(they are not valid for BREs):
- expression+
- Matches what one or more occurrences of expression matches. For example,
a+(bc) matches aaaaabc; (bc)+ matches characters 1
through 6 of bcbcbcbb.
- expression?
- Matches zero or one consecutive occurrences of what expression matches.
For example, b?c matches character 2 of acabbb (zero occurrences
of b followed by c).
- expression|expression
- Matches a string that matches either expression. For example, a((bc)|d)
matches both abc and ad.
|