SI+GCC
时间:2010-07-15 来源:chiang
\( and \)
Parts of a regular expression can be isolated by enclosing them with \( and \), thereby forming a group. Groups are useful for extracting part of a match to be used in a replacement pattern. Each group in a pattern is assigned a number, starting with 1, from left to right.
Example: abc\(xyz\) matches abcxyz. xyz is considered group #1.
This is not all that useful, unless we are using the Replace command. The replace string can contain group characters in the form of \<number>. Each time a group character is encountered in the replacement pattern, it means “substitute the group value from the matched pattern”.
Example 1: replace \(abc\)\(xyz\) with \2\1. This replaces the matched string abcxyz with the contents of group #2 xyz, followed by the contents of group #1 abc. So abcxyz is replaced with xyzabc. This is still not too amazing. See the next example.
Example 2: replace \(\w+\)\(.*\)ing with \1\2ed. This changes words ending in ing with the same word ending with ed. Your English teacher would not be too happy.