Macaulay2 » Documentation
Packages » Macaulay2Doc > The Macaulay2 language > strings and nets > regular expressions > select(String,String,String)
next | previous | forward | backward | up | index | toc

select(String,String,String) -- select and reformat substrings matching a regular expression

Synopsis

Description

For an introduction to regular expressions, see regular expressions.

i1 : select("[[:alpha:]]+", "Dog, cat, and deer.")

o1 = {Dog, cat, and, deer}

o1 : List
i2 : select("^.*$", "ABC\nDEF\r\nGHI")

o2 = {ABC, DEF, GHI}

o2 : List

The replacement string may contain formatting syntax such as backreferences $1 or \\1, which will be replaced by the string matching the corresponding parenthesized subexpression of re.

i3 : select("([a-zA-Z]+);", "$1", "Dog; cat, deer;")

o3 = {Dog, deer}

o3 : List

Special operators such as the lowercase operator \\L may also be used to manipulate the replacement substring.

i4 : select("([a-zA-Z]+);", "\\L$1", "Dog; cat, deer;")

o4 = {dog, deer}

o4 : List

Lookaheads and lookbehinds can be used to precisely control the regular expression matches.

i5 : s = "catfish cats dogs";
i6 : select("cat(?!fish)s?", s)

o6 = {cats}

o6 : List
i7 : select("\\w+(?=s\\b)", s)

o7 = {cat, dog}

o7 : List
i8 : s = "goldfish swordfish catfish catdog";
i9 : select("\\w+(?=fish)", s)

o9 = {gold, sword, cat}

o9 : List
i10 : select("(?<=cat)\\w+", s)

o10 = {fish, dog}

o10 : List

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 lookaheads and lookbehinds.

See also

Ways to use this method: