[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
13.2.8 Clusters
Clustering, ie, enclosure within parens (
...)
,
identifies the enclosed subpattern as a single entity. It causes
the matcher to capture the submatch, or the portion of the
string matching the subpattern, in addition to the overall match.
(pregexp-match "([a-z]+) ([0-9]+), ([0-9]+)" "jan 1, 1970") ⇒ ("jan 1, 1970" "jan" "1" "1970")
Clustering also causes a following quantifier to treat the entire enclosed subpattern as an entity.
(pregexp-match "(poo )*" "poo poo platter") ⇒ ("poo poo " "poo ")
The number of submatches returned is always equal to the number of subpatterns specified in the regexp, even if a particular subpattern happens to match more than one substring or no substring at all.
(pregexp-match "([a-z ]+;)*" "lather; rinse; repeat;") ⇒ ("lather; rinse; repeat;" " repeat;")
Here the *
-quantified subpattern matches three
times, but it is the last submatch that is returned.
It is also possible for a quantified subpattern to
fail to match, even if the overall pattern matches.
In such cases, the failing submatch is represented
by #f
.
(define date-re ;match `month year' or `month day, year'. ;subpattern matches day, if present (pregexp "([a-z]+) +([0-9]+,)? *([0-9]+)")) (pregexp-match date-re "jan 1, 1970") ⇒ ("jan 1, 1970" "jan" "1," "1970") (pregexp-match date-re "jan 1970") ⇒ ("jan 1970" "jan" #f "1970")
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.