manpagez: man pages & more
info bigloo
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.2.9 Backreferences

Submatches can be used in the insert string argument of the procedures pregexp-replace and pregexp-replace*. The insert string can use \n as a backreference to refer back to the nth submatch, ie, the substring that matched the nth subpattern. \0 refers to the entire match, and it can also be specified as \&.

(pregexp-replace "_(.+?)_" 
  "the _nina_, the _pinta_, and the _santa maria_"
 ⇒ "the *nina*, the _pinta_, and the _santa maria_"

(pregexp-replace* "_(.+?)_" 
  "the _nina_, the _pinta_, and the _santa maria_"
 ⇒ "the *nina*, the *pinta*, and the *santa maria*"

;recall: \S stands for non-whitespace character

(pregexp-replace "(\\S+) (\\S+) (\\S+)"
  "eat to live"
  "\\3 \\2 \\1")
 ⇒ "live to eat"

Use \\ in the insert string to specify a literal backslash. Also, \$ stands for an empty string, and is useful for separating a backreference \n from an immediately following number.

Backreferences can also be used within the regexp pattern to refer back to an already matched subpattern in the pattern. \n stands for an exact repeat of the nth submatch.(7)

(pregexp-match "([a-z]+) and \\1"
  "billions and billions")
 ⇒ ("billions and billions" "billions")

Note that the backreference is not simply a repeat of the previous subpattern. Rather it is a repeat of the particular substring already matched by the subpattern.

In the above example, the backreference can only match billions. It will not match millions, even though the subpattern it harks back to — ([a-z]+) — would have had no problem doing so:

(pregexp-match "([a-z]+) and \\1"
  "billions and millions")
 ⇒ #f 

The following corrects doubled words:

(pregexp-replace* "(\\S+) \\1"
  "now is the the time for all good men to to come to the aid of of the party"
 ⇒ "now is the time for all good men to come to the aid of the party"

The following marks all immediately repeating patterns in a number string:

(pregexp-replace* "(\\d+)\\1"
 ⇒ "12{3,3}40983{24,24}3242{098,098}0234"

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on October 23, 2011 using texi2html 5.0.