This post shows how to use most of the functions in the Regex class, work with null safely related to the Regex functions, and how raw strings makes it easier to write and read regex patterns.

The RegEx class

To work with regular expressions in Kotlin, you need to use the Regex(pattern: String) class and invoke functions like find(..) or replace(..) on that regex object.

An example on how to use the Regex class that returns true if the input string contains c or d:

val regex = Regex(pattern = "c|d")
val matched = regex.containsMatchIn(input = "abc")    // matched: true

The essential thing to understand with all the Regex functions is that the result is based on matching the regex pattern and the input string. Some of the functions requires a full match, while the rest requires only a partial match. The containsMatchIn(..) function used in the example requires a partial match and is explained later in this post.

Null safety with regular expressions

Both find(..) and matchEntire(..) will return a MatchResult? object. The ? character after MatchResult is necessary for Kotlin to handle null safely.

An example that demonstrates how Kotlin handles null safely from a Regex function, when the find(..) function returns null:

val matchResult = 
    Regex("c|d").find("efg")           // matchResult: null
val a = matchResult?.value             // a: null
val b = matchResult?.value.orEmpty()   // b: ""
a?.toUpperCase()                       // Still needs question mark. => null    
b.toUpperCase()                        // Accesses the function directly. => ""

With the orEmpty() function, b can’t be null and the ? character is unnecessary when you call functions on b.

If you don’t care about this safe handling of null values, Kotlin allows you to work with null values like in Java with the !! characters:

a!!.toUpperCase()                      // => KotlinNullPointerException

Raw strings in regex patterns

Kotlin provides an improvement over Java with a raw string that makes it possible to write pure regex patterns without double backslashes, that are necessary with a Java string. A raw string is represented with a triple quote:

"""\\d{3}-\\d{3}-\\d{4}""" // raw Kotlin string
"\\\\d{3}-\\\\d{3}-\\\\d{4}"  // standard Java string

find(input: CharSequence, startIndex: Int): MatchResult?

The input string will be matched against the pattern in the Regex object. It returns a Matchresult? object with the first matched text after the startIndex, or null if the pattern didn’t match the input string. The result string is retrieved from the MatchResult? object’s value property. The startIndex parameter is optional with the default value 0.

To extract the first valid phone number from a string with contact details:

val phoneNumber :String? = Regex(pattern = """\\d{3}-\\d{3}-\\d{4}""")
    .find(input = "phone: 123-456-7890, e..")?.value // phoneNumber: 123-456-7890