A class for cross-platform regular expressions modeled on Javascript's RegExp, plus some extra methods like Java's and Javascript String's replace and split methods (taking a RegExp parameter) that are missing from Ceylon's version of String. To create an instance of this class use the toplevel function regex().

Example usage:

Regex re = regex("[0-9]+ years");
assert(re.test("90 years old"));
print(re.replace("90 years old", "very"));

There are a few small incompatibilities between the two implementations. Java-specific constructs in the regular expression syntax (e.g. [a-z&&[^bc]], (?<=foo), \A, \Q) work only on the JVM backend, while the Javascript-specific constructs $` and $' in the replacement expression work only on the Javascript backend, not the JVM backend, which rejects them. There are also sure to exist small differences between the different browser implementations, be sure to test thoroughly, especially when using more advanced features.

no subtypes hierarchy

Initializer
Regex(String expression, Boolean global = false, Boolean ignoreCase = false, Boolean multiLine = false)
Parameters:
  • expression

    The regular expression to be used for all operations

  • global = false

    For returning all matches instead of only the first

  • ignoreCase = false

    For case-insensitive matching

  • multiLine = false

    For multi-line matching where ^ and $ also match line delimiters and not just the beginning or end of the entire input string

Attributes
expressionSource Codeshared String expression

The regular expression to be used for all operations

globalSource Codeshared Boolean global

For returning all matches instead of only the first

ignoreCaseSource Codeshared Boolean ignoreCase

For case-insensitive matching

lastIndexSource Codeshared formal variable Integer lastIndex

The zero-based position at which to start the next match

multiLineSource Codeshared Boolean multiLine

For multi-line matching where ^ and $ also match line delimiters and not just the beginning or end of the entire input string

Inherited Attributes
Attributes inherited from: Object
Methods
findSource Codeshared formal MatchResult? find(String input)

Applies the regular expression to the given string. This call affects the value returned by lastIndex if the global flag is set. Produces a match result if the string matches, else null.

Parameters:
  • input

    the string to apply the regular expression to

findAllSource Codeshared default MatchResult[] findAll(String input)

Applies the regular expression to the given string. Produces a sequence of match result containing all matches, or Empty if there was no match.

replaceSource Codeshared default String replace(String input, String|String(String)|String(MatchResult) replacement)

Returns the input string with the part(s) matching the regular expression replaced with the replacement string or the value returned by the replacement function. If the global flag is set, replaces all matches of the regular expression. Otherwise, replaces the first match of the regular expression.

When using a replacement string, as per Javascript semantics, backslashes in the replacement string get no special treatment, but the replacement string can use the following special patterns:

  • $1, $2, … $99 - inserts the n'th group matched by the regular expression.
  • $& - inserts the entire string matched by the regular expression.
  • $$ - inserts a $.

Note: “$`” and “$'” are not supported in the pure Java implementation, and throw an exception.

When using a replacement function, if the replacement function accepts a String parameter, this function will pass it the string that matched. If the replacement function accepts a MatchResult parameter, this function will pass it the entire MatchResult instance.

Parameters:
  • input

    the string in which the regular expression is to be searched

  • replacement

    the replacement string or function

replaceDollarSignPatternSource Codeshared formal String replaceDollarSignPattern(String input, String replacement)

Replaces dollar sign patterns as described in the documentation for replace().

splitSource Codeshared formal String[] split(String input, Integer limit = -1)

Splits the input string around matches of the regular expression. If the regular expression is completely empty, splits the input string into its constituent characters. If the regular expression is not empty but matches an empty string, the results are not well defined.

Note: There are some browser inconsistencies with this implementation, as it is delegated to the browser, and no browser follows the spec completely. A major difference is that IE will exclude empty strings in the result.

Parameters:
  • input

    the string to be split

  • limit = -1

    the maximum number of strings to split off and return, ignoring the rest of the input string. If negative, there is no limit

testSource Codeshared default Boolean test(String input)

Determines if the regular expression matches the given string. This call affects the value returned by lastIndex if the global flag is set. Equivalent to: exec(input) != null

Parameters:
  • input

    the string to apply the regular expression to

Inherited Methods
Methods inherited from: Object