# match -- regular expression matching

## Synopsis

• Usage:
match(re, str)
match(patterns, str)
• Inputs:
• re, , a regular expression describing a pattern
• str, , a subject string to be searched
• patterns, a list, a list of regular expressions
• Optional inputs:
• POSIX => , default value false, if true, interpret the re using the POSIX Extended flavor, otherwise the Perl flavor
• Strategy => , default value any, logical quantifier for matching a list of patterns, typically all or any
• Outputs:
• , whether the string str is a match for the regular expression re, or at least one of patterns when Strategy => any (default)
• Consequences:

## Description

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("(?

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

Optionally, 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.

• regular expressions -- syntax for regular expressions
• strings and nets -- an overview of strings and nets in Macaulay2
• regex -- evaluate a regular expression search
• replace -- replacement in strings and lists
• substring -- extract part of a string
• any -- whether any elements satisfy a specified condition
• all -- whether all elements satisfy a specified condition

## Ways to use match :

• "match(List,String)"
• "match(String,String)"

## For the programmer

The object match is .