next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc > The Macaulay2 language > strings and nets > regular expressions > match

match -- regular expression matching



For an introduction to regular expressions, see regular expressions.

i1 : s = "three dogs, two catfishes, and a cat"

o1 = three dogs, two catfishes, and a cat
i2 : match("cat", s)

o2 = true
i3 : lastMatch

o3 = {(16, 3)}

o3 : List
i4 : substring(first lastMatch, s)

o4 = cat
i5 : match ("cats", s)

o5 = false
i6 : lastMatch

The POSIX => true option can be used to specify the POSIX Extended flavor for the regular expression used to match. Note that only the Perl flavor allows the use of lookahead and lookbehinds.

i7 : s = "catfish cat dog"

o7 = catfish cat dog
i8 : match("cat(?!fish)", s)

o8 = true
i9 : substring(lastMatch#0#0, lastMatch#0#1 + 4, s)

o9 = cat dog
i10 : match("cat(?=fish)", s)

o10 = true
i11 : substring(lastMatch#0#0, lastMatch#0#1 + 4, s)

o11 = catfish
i12 : match("(?<!cat)fish", "cat catfish dog")

o12 = false
i13 : match("(?<!cat)fish", "cat swordfish dog")

o13 = true

When the first input is a list, by default the output is true if str is a match for at least one of the given regular expressions.

i14 : match({"Cat", "Dog"}, "CatDog")

o14 = true
i15 : match({"Cat", "Dog"}, "Catfish")

o15 = true

Optinally, Strategy => all indicates that the string should match every pattern in the list to be a match.

i16 : match({"Cat", "Dog"}, "CatDog", Strategy => all)

o16 = true
i17 : not match({"Cat", "Dog"}, "Catfish", Strategy => all)

o17 = true

The Strategy option is not used when the first input is not a list.

See also

Ways to use match :

For the programmer

The object match is a method function with options.