- 论坛徽章:
- 0
|
回复 #3 bequan 的帖子
Backreferences
Closely associated with the matching variables $1 , $2 , ... are the backreferences \1 , \2 ,... Backreferences are simply matching variables that can be used inside a regexp. This is a really nice feature -- what matches later in a regexp is made to depend on what matched earlier in the regexp. Suppose we wanted to look for doubled words in a text, like 'the the'. The following regexp finds all 3-letter doubles with a space in between:
1. /\b(\w\w\w)\s\1\b/;
The grouping assigns a value to \1, so that the same 3 letter sequence is used for both parts.
A similar task is to find words consisting of two identical parts:
1. % simple_grep '^(\w\w\w\w|\w\w\w|\w\w|\w)\1$' /usr/dict/words
2. beriberi
3. booboo
4. coco
5. mama
6. murmur
7. papa
The regexp has a single grouping which considers 4-letter combinations, then 3-letter combinations, etc., and uses \1 to look for a repeat. Although $1 and \1 represent the same thing, care should be taken to use matched variables $1 , $2 ,... only outside a regexp and backreferences \1 , \2 ,... only inside a regexp; not doing so may lead to surprising and unsatisfactory results. |
|