pnuts.regex Module Functions

match (regular_expression, String input {, String options })

match() checks if the String input includes a regular_expression and returns the result.

regular_expression can be either a String or a Pattern object of a particular regex API. The syntax of regular expression depends on the regex API.

e.g.
match("a*b", "aaaaabbb")  => true

options is a string of the following characters.

optionmeaning
i Case-insensitive pattern matching
m Treats string as multiple lines
s Treats string as single line. "." matches any single character
n Meta characters are escaped.
other Passed to a particular Regex API
e.g.
match("a", "ABC", "i")  => true
getMatch (int index)

getMatch(0) returns the previously matched string. getMatch(n) returns the string that matches the Nth regex group.

e.g.
match("(a*)b(c*)", "aaabcc")  => true

getMatch(0)  => "aaabcc"
getMatch(1)  => "aaa"
getMatch(2)  => "cc"
getMatches ( )

getMatches() returns an array of matched string with pattern groups.

e.g.
match("(a*)b(c*)", "aaabcc")  => true

getMatches()  => ["aaa", "cc"]
getMatchStart (int index)

getMatchStart(0) returns the start index of previously matched string. getMatchStart(n) returns the start index of string that matches the Nth regex group.

e.g.
match("(a*)b(c*)", "aaabcc")  => true

getMatchStart(0)  => 0
getMatchStart(1)  => 0
getMatchStart(2)  => 4
getMatchEnd (int index)

getMatchEnd(0) returns the end index of previously matched string. getMatchEnd(n) returns the end index of string that matches the Nth regex group.

e.g.
match("(a*)b(c*)", "aaabcc")  => true

getMatchEnd(0)  => 6
getMatchEnd(1)  => 3
getMatchEnd(2)  => 6
getNumberOfGroups ()

getNumberOfGroups() returns the number of parenthesized subexpressions available after a successful match.

The result of this function is undefined if it is called after a unsuccessful match.

formatMatch ( String replacement_string )

formatMatch() formats the replacement_string, substituting captured subsequences of the previous pattern match.

e.g.
match("(a+)(b+)", "aaaabbbb")
formatMatch("$2$1")
  => "bbbbaaaa"
matchAll (regular_expression, String input { , func (word) { , String options } }

If func is specified, matchAll() searches the regular_expression in input and calls func passing the matched words in turn, then returns the number of matches.

If func is not specified, it returns an Iterator that can be used to get the matched words.

e.g.
for (word : matchAll("[a-z]+", "abc def")){
   println(word)
}
subsitute (regular_expression, String replacement, String input {, String options }) or
(regular_expression, func( {matchedWord} ) , String input {, String options })

When a string is specified as the 2nd parameter, substitute() replaces the string which matches the regular_expression with replacement. When 'n' is included in options, meta characters in replacement, if any, have no effect.

When a function is specified as the 2nd parameter, substitute() replaces the string which matches the regular_expression with the result of the function call. The function must be able to take zero or one argument. If the function takes one argument, matched word is passed as the argument.

regular_expression can be either a String or a Pattern object of a particular regex API. The syntax of regular expression depends on the regex API.

e.g.
substitute("[a-zA-Z]+", {x-> toUpperCase(x)}, "aBcDe")  => "ABCDE"
split (regular_expression, String input {, String options })

split() tokenizes a string with the regular_expression as the delimiter. It returns a java.util.List object which contains the resulting tokens.

regular_expression can be either a String or a Pattern object of a particular regex API. The syntax of regular expression depends on the regex API.

e.g.
split(`\.`, "a.b.c") => ["a", "b", "c"]
whichRegexAPI ( )

whichRegexAPI() returns the module name which is currently used. It returs null if no scripts is loaded.

getMatchCount ( )

getMatchCount() returns the number of substitution done by previous substitute() call.

regex ( String expression {, String options } )

regex() makes a Pattern object of a particular regex API from expression. For example, when using pnuts.regex.jsr51 module, it returns a java.util.regex.Pattern object.