A regular expression is a pattern that the regular expression engine attempts to match in input text. A pattern consists of one or more character literals, operators, or constructs.
Alternation Constructs
Alternation constructs modify a regular expression to enable either/or matching. These constructs include the language elements listed in the following table.
Alternation construct | Description | Pattern | Matches |
---|---|---|---|
| | Matches any one element separated by the vertical bar (|) character. | th(e|is|at) | "the", "this" in "this is the day. " |
(?( expression ) yes | no ) | Matches yes if the regular expression pattern designated by expression matches; otherwise, matches the optional no part. expression is interpreted as a zero-width assertion. | (?(A)A\d{2}\b|\b\d{3}\b) | "A10", "910" in "A10 C103 910" |
(?( name ) yes | no ) | Matches yes if name, a named or numbered capturing group, has a match; otherwise, matches the optional no. | (?<quoted>")?(?(quoted).+?"|\S+\s) | Dogs.jpg, "Yiska playing.jpg" in "Dogs.jpg "Yiska playing.jpg"" |
Substitutions
Substitutions are regular expression language elements that are supported in replacement patterns.
Character | Description | Pattern | Replacement pattern | Input string | Result string |
---|---|---|---|---|---|
$ number | Substitutes the substring matched by group number. | \b(\w+)(\s)(\w+)\b | $3$2$1 | "one two" | "two one" |
${ name } | Substitutes the substring matched by the named group name. | \b(?<word1>\w+)(\s)(?<word2>\w+)\b | ${word2} ${word1} | "one two" | "two one" |
$$ | Substitutes a literal "$". | \b(\d+)\s?USD | $$$1 | "103 USD" | "$103" |
$& | Substitutes a copy of the whole match. | (\$*(\d*(\.+\d+)?){1}) | **$& | "$1.30" | "**$1.30**" |
$` | Substitutes all the text of the input string before the match. | B+ | $` | "AABBCC" | "AAAACC" |
$' | Substitutes all the text of the input string after the match. | B+ | $' | "AABBCC" | "AACCCC" |
$+ | Substitutes the last group that was captured. | B+(C+) | $+ | "AABBCCDD" | AACCDD |
$_ | Substitutes the entire input string. | B+ | $_ | "AABBCC" | "AAAABBCCCC" |
No comments:
Post a Comment