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

regular expressions

A regular expression is a string that specifies a pattern that describes a set of matching subject strings. Regular expressions are constructed inductively as follows. Ordinary (non-special) characters match themselves. A concatenation of regular expressions matches the concatenation of corresponding matching subject strings. Regular expressions separated by the character | match strings matched by any. Parentheses can be used for grouping, and results about which substrings of the target string matched which parenthesized subexpression of the regular expression can be returned.

The special characters are those appearing in the following constructions. The special character \ may be confusing, as inside a string delimited by quotation marks ("..."), you type two of them to get one, whereas inside a string delimited by triple slashes (///...///), you type one to get one. Thus regular expressions delimited by triple slashes are more readable.

There are the following character classes. In order to match one of the special characters itself, precede it with a backslash.

We illustrate the use of regular expressions with regex(String,String).

i1 : regex("d", "1abcddddeF2")

o1 = {(4, 1)}

o1 : List
i2 : regex("d*", "1abcddddeF2")

o2 = {(0, 0)}

o2 : List
i3 : regex("d+", "1abcddddeF2")

o3 = {(4, 4)}

o3 : List
i4 : regex("d+", "1abceF2")
i5 : regex("cdd+e", "1abcddddeF2")

o5 = {(3, 6)}

o5 : List
i6 : regex("cd(d+)e", "1abcddddeF2")

o6 = {(3, 6), (5, 3)}

o6 : List
i7 : regex("[a-z]+", "1abcddddeF2")

o7 = {(1, 8)}

o7 : List
i8 : regex("[[:alpha:]]+", "Dog cat cat.")

o8 = {(0, 3)}

o8 : List
i9 : regex("([[:alpha:]]+) *\\1","Dog cat cat.")

o9 = {(4, 7), (4, 3)}

o9 : List
For complete documentation on regular expressions see the entry for regex in section 7 of the unix man pages, or read the the GNU regex manual.

Menu

functions that accept regular expressions