1 Welcome to PLT Scheme
2 Scheme Essentials
3 Built-In Datatypes
4 Expressions and Definitions
5 Programmer-Defined Datatypes
6 Modules
7 Contracts
8 Input and Output
9 Regular Expressions
10 Exceptions and Control
11 Iterations and Comprehensions
12 Pattern Matching
13 Classes and Objects
14 Units (Components)
15 Reflection and Dynamic Evaluation
16 Macros
17 Performance
18 Running and Creating Executables
19 Compilation and Configuration
20 More Libraries
Bibliography
Index
Version: 4.0.2

 

9.7 Alternation

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).

  > (regexp-match #rx"f(ee|i|o|um)" "a small, final fee")

  ("fi" "i")

  > (regexp-replace* #rx"([yi])s(e[sdr]?|ing|ation)"

                     (string-append

                      "analyse an energising organisation"

                      " pulsing with noisy organisms")

                     "\\1z\\2")

  "analyze an energizing organization pulsing with noisy organisms"

Note again that if you wish to use clustering merely to specify a list of alternate subpatterns but do not want the submatch, use (?: instead of (.

  > (regexp-match #rx"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.

  > (regexp-match #rx"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:

  > (regexp-match #rx"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 non-match. In the following, the longer alternate still wins, because its preferred shorter prefix fails to yield an overall match.

  > (regexp-match

     #rx"(?:call|call-with-current-continuation) constrained"

     "call-with-current-continuation constrained")

  ("call-with-current-continuation constrained")