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

separateRegexp -- split a string into substrings, using a regular expression to define the separators



For an introduction to regular expressions, see regular expressions.

Example 1: use commas, periods, and semicolons as separators.

i1 : separateRegexp("[,.;]", "Example: a string. That, is punctuated, weirdly; for demonstration purposes.")

o1 = {Example: a string,  That,  is punctuated,  weirdly,  for demonstration
     purposes, }

o1 : List

Example 2: match any number of consecutive spaces.

i2 : separateRegexp("[ ]+", "this    string has   different   lengths of    spacing  between     words")

o2 = {this, string, has, different, lengths, of, spacing, between, words}

o2 : List

Example 3: delete every word starting with "x" from a string, by using concatenate together with separateRegexp.

i3 : s = "algng xjfr kfjxse xhgfj xooi xwj kvexr anvi endj xkfi";
i4 : concatenate(separateRegexp(" x[A-Za-z]*", s))

o4 = algng kfjxse kvexr anvi endj

Example 4: The optional argument n allows us to specify a separator that differs from the match criteria. In the previous example, words beginning with "x" were both the match and the separator. In this example, we match words beginning with "x", but separate the string using the leading "x". With concatenate, this results in deleting just the "x" from words starting with "x" (not the same as removing every "x").

i5 : concatenate(separateRegexp(" (x)[A-Za-z]*", 1, s))

o5 = algng jfr kfjxse hgfj ooi wj kvexr anvi endj kfi

Example 5: match words beginning with "x", but use the rest of the word as the separator.

i6 : separateRegexp(" (x)([A-Za-z]*)", 2, s)

o6 = {algng x,  kfjxse x,  x,  x,  kvexr anvi endj x, }

o6 : List

See also

Ways to use separateRegexp :