Skip to main content

Regex.split

splitinput only_first

Group: Conversions

Documentation

Splits the input text based on the pattern described by self. This method will always return a vector. If no splits take place, the vector will contain a single element (equal to the original string).

Arguments

  • input: The text to split based on the pattern described by self.
  • only_first: If true, only split at the first occurrence.

Examples

Split on the first instance of the pattern.

      pattern = Regex.compile "cd"
input = "abcdefcdghij"
texts = pattern.split input only_first=True
texts . should_equal ["ab", "efcdghij"]

Split on the all instances of the pattern in the input.

     pattern = Regex.compile "a"
input = "bacadaeaf"
texts = pattern.split input
texts . should_equal ["b", "c", "d", "e", "f"]

Returns the original text if there are no matches.

      pattern = Regex.compile "aa"
input = "abcdefghij"
texts = pattern.split input
texts . should_equal ["abcdefghij"]