next | previous | forward | backward | up | top | index | toc | Macaulay2 web site
Macaulay2Doc > The Macaulay2 language > strings and nets > regular expressions > regex

regex -- define and evaluate a regular expression

Synopsis

Description

The value returned is a list of pairs of integers corresponding to the parenthesized subexpressions successfully matched, suitable for use as the first argument of substring. The first member of each pair is the offset within s of the substring matched, and the second is the length.
i1 : s = "The cat is black.";
i2 : word = ///\b([a-z]+)\b///;
i3 : m = regex(word|" "|word,s)

o3 = {(4, 6), (4, 3), (8, 2)}

o3 : List
i4 : substring(m#0,s)

o4 = cat is
i5 : substring(m#1,s)

o5 = cat
i6 : substring(m#2,s)

o6 = is
i7 : s = "aa     aaaa";
i8 : m = regex("a+",0,s)

o8 = {(0, 2)}

o8 : List
i9 : substring(m#0,s)

o9 = aa
i10 : m = regex("a+",2,s)

o10 = {(7, 4)}

o10 : List
i11 : substring(m#0,s)

o11 = aaaa
i12 : m = regex("a+",2,3,s)
i13 : s = "line 1\nline 2\nline 3";
i14 : m = regex("^.*$",8,-8,s)

o14 = {(7, 6)}

o14 : List
i15 : substring(m#0,s)

o15 = line 2
i16 : m = regex("^",10,-10,s)

o16 = {(7, 0)}

o16 : List
i17 : substring(0,m#0#0,s)

o17 = line 1
i18 : substring(m#0#0,s)

o18 = line 2
      line 3
i19 : m = regex("^.*$",4,-10,s)

o19 = {(0, 6)}

o19 : List
i20 : substring(m#0,s)

o20 = line 1
i21 : m = regex("a.*$",4,-10,s)

See also

Ways to use regex :