Text
This is a block of of text, several words here are are repeated, and and they should not be.
RegEx
[ ]+(/w+)[ ]+/1
Result
This is a block of of text, several words here are are repeated, and and they should not be.
AnalysisThe pattern apparently worked, but how did it work? [ ]+ matches one or more spaces,
/w+ matches one or more alphanumeric characters, and [ ]+ then matches any trailing spaces.
But notice that /w+ is enclosed within parentheses, making it a subexpression.
This subexpression is not used for repeating matches; there is no repeat matching here.
Rather, the subexpression is used simply to group an expression,
to flag it and identify it for future use. The final part of this pattern is /1 ;
this is a reference back to the subexpression, and so when (/w+) matched the word of , so did /1 ,
and when (/w+) matched the word and , so did /1 .
例子二:结合引用进行替代(使用“$”)
Text313-555-1234 248-555-9999 810-555-9000RegEx(/d{3})(-)(/d{3})(-)(/d{4})Replace($1) $3-$5Result(313) 555-1234 (248) 555-9999 (810) 555-9000AnalysisAgain, two regular expression patterns are used here. The first looks far more complicated than it is, so let's walk through it.
(/d{3})(-)(/d{3})(-)(/d{4}) matches a phone number, but breaks it into five subexpressions (so as to isolate its parts).
(/d{3}) matches the first three digits as the first subexpression, (-) matches – as the second subexpression, and so on.
The end result is that the phone number is broken into five parts (each part its own subexpression): the area code, a hyphen,
the first three digits of the number, another hyphen, and then the final four digits. These five parts can be used individually and as needed,
and so ($1) $3-$5 simply reformats the number using only three of the subexpressions and ignoring the other two,
thereby turning 313-555-1234 into (313) 555-1234 .