In Kotlin, regular expressions are represented by the Regex class from the kotlin.text package. Instances of this class are immutable, and we can create a new one by using one of the following constructors:
public actual constructor(pattern: String) : this(Pattern.compile(pattern))
public actual constructor(pattern: String, option: RegexOption) : this(Pattern.compile(pattern, ensureUnicodeCase(option.value)))
public actual constructor(pattern: String, options: Set<RegexOption>) : this(Pattern.compile(pattern, ensureUnicodeCase(options.toInt())))
RegexOption is an enum that we can use to specify any additional options. This defines the following objects:
- IGNORE_CASE: You can use this if you need to ignore a string
- MULTILINE: This is a line terminator or the end of the input sequence, and it must be used just after or just before ^ and $ subexpressions
- LITERAL: This ignores all special symbols and metacharacters
- UNIX_LINES: With this, only the means a line terminator
- COMMENTS: This allows you to use white space and comments in the pattern
- DOT_MATCHES_ALL: This allows the . subexpression to match any character
- CANON_EQ: This enables special equivalence by canonical decomposition
We can also use the toRegex() extension function to create a new instance:
"^The".toRegex()
We can also use the containsMatchIn method that returns true if the regular expression can find at least one match in the specified input.
We can use these functions as follows:
fun main(args: Array<String>) {
println("^The".toRegex().containsMatchIn("The"))
}
The output is as follows:
true
Another useful method is split, which allows you to split an input into a list of substrings. This method can be used as follows:
fun main(args: Array<String>) {
println("""d""".toRegex().split("abc2abc4abc"))
}
The following is the output:
[abc, abc, abc]