Next: pregexp syntax backtrack, Previous: pregexp syntax clusters, Up: pregexp syntax [Index]
You can specify a list of alternate subpatterns by separating them by
|
. The |
separates subpatterns in the nearest enclosing
cluster (or in the entire pattern string if there are no enclosing
parens).
(pregexp-match "f(ee|i|o|um)" "a small, final fee") ⇒ ("fi" "i") (pregexp-replace* "([yi])s(e[sdr]?|ing|ation)" "it is energising to analyse an organisation pulsing with noisy organisms" "\\1z\\2") ⇒ "it is energizing to analyze an organization pulsing with noisy organisms"
Note again that if we wish to use clustering merely to specify a list of
alternate subpatterns but do not want the submatch, use (?:
instead of (
.
(pregexp-match "f(?:ee|i|o|um)" "fun for all") ⇒ ("fo")
An important thing to note about alternation is that the leftmost matching alternate is picked regardless of its length. Thus, if one of the alternates is a prefix of a later alternate, the latter may not have a chance to match.
(pregexp-match "call|call-with-current-continuation" "call-with-current-continuation") ⇒ ("call")
To allow the longer alternate to have a shot at matching, place it before the shorter one:
(pregexp-match "call-with-current-continuation|call" "call-with-current-continuation") ⇒ ("call-with-current-continuation")
In any case, an overall match for the entire regexp is always preferred to an overall nonmatch. In the following, the longer alternate still wins, because its preferred shorter prefix fails to yield an overall match.
(pregexp-match "(?:call|call-with-current-continuation) constrained" "call-with-current-continuation constrained") ⇒ ("call-with-current-continuation constrained")