(→Groups and Replacement Examples) |
(→Related Resources) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 41: | Line 41: | ||
:Delete all text up to a comma on the next line which contains a comma. (Remember that the entire matching text must occur on one line.) | :Delete all text up to a comma on the next line which contains a comma. (Remember that the entire matching text must occur on one line.) | ||
;Search<nowiki>:</nowiki> {[Hh]}ello | ;Search<nowiki>:</nowiki> {[Hh]}ello | ||
| − | Replace<nowiki>:</nowiki> \1i | + | ;Replace<nowiki>:</nowiki> \1i |
| + | :Searches for “Hello” or “hello”, and replaces it with “Hi” or “hi” respectively. | ||
| + | ;Search<nowiki>:</nowiki> [Hh]ello | ||
| + | ;Replace<nowiki>:</nowiki> &~world | ||
| + | :Searches for “Hello” or “hello”, and replaces it with “Hello~world” or “hello~world” respectively. The “'''&'''” references the entire text matched by the search. Note that the grouping characters “'''{ }'''” are NOT needed in order to use “'''&'''”. | ||
==Related Resources== | ==Related Resources== | ||
| + | *[[Regular_Expressions|Regular Expressions Overview]] | ||
| + | *[[Regular_Expressions_Basics|Regular Expressions Basics]] | ||
| + | *[[Special_Matching_Characters|Special Matching Characters]] | ||
| + | *[[Special_Matching_Characters#The_OR_Operator|The “OR” Operator]] | ||
| + | *[[Groups_Replacement_Strings#Groups_and_Replacement_Examples|Groups and Replacement Examples]] | ||
| + | *[[Regular_Expressions_Newline|Matching the “Newline”]] | ||
| + | *[[Maximize_Regular_Expression|Maximize Regular Expression Matching]] | ||
The special symbols “{” and “}” group regular expressions for reference purposes. They permit the text matched by the expression within “{ }” to be referenced again in the search string or to be included as part of the replacement text.
An expression may contain up to 9 groups which are referenced by number, “\1” through “\9”. The groups are numbered in the order of their opening “{”.Groups may also be nested. Groups may be referenced in either a latter part of the regular expression or in the replacement string. This allows portions of the matched text to be used as parts of the replacement text.
The character “&” has a special meaning only in a replacement string and references the entire text matched by the search.
Consider the expression “the {man}|the {woman}”. If the matched text is “the man”, “\1” is “man” and “\2” is empty (null). Now consider the expression “{the {[a-z]+}} has”. If the matched text is “the woman has”, “\1” is “thewoman” and “\2” is “woman”.
The order of precedence of the regular expressions operators is:
Highest > (to) Lowest
\\ > [ ] > * + ? > { } > Concatenations > |
These examples show search and replacement strings using regular expressions. They illustrate how groups of matched text can be used as part of the replacement string. Be sure that the option ( ) Reg-Exp is selected in the Search/Replace dialog box.