EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Kotlin

Regex & Ranges

Kotlin provides robust support for regular expressions through the Regex class, which allows for efficient string pattern matching. A Regex object represents a regular expression and can be used for various string-matching purposes.

Constructors:

  • Regex(pattern: String): Creates a regular expression from the provided pattern.
  • Regex(pattern: String, option: RegexOption): Creates a regular expression with a specific option from the RegexOption enum.
  • Regex(pattern: String, options: Set<RegexOption>): Creates a regular expression with a set of options.

Properties:

  • val options: Set<RegexOption>: Contains the set of options used for regex creation.
  • val pattern: String: Stores the pattern as a string.

Functions in Regex

1. containsMatchIn(): This function checks if there is a match for the regex pattern within a given input and returns a boolean.

fun main() {
    val pattern = Regex("^a")  // Matches any string starting with 'a'
    println(pattern.containsMatchIn("abc"))  // true
    println(pattern.containsMatchIn("bac"))  // false
}

Output:

fun main() {
    val pattern = Regex("^a")  // Matches any string starting with 'a'
    println(pattern.containsMatchIn("abc"))  // true
    println(pattern.containsMatchIn("bac"))  // false
}

2. find(): This function returns the first match of the regex in the input starting from a specified index.

fun main() {
    val pattern = Regex("ll")  // Matches "ll"
    val match: MatchResult? = pattern.find("HelloHello", 5)
    println(match?.value)  // ll
}

Output:

ll

3. findAll() : This function finds all the matches for a regex in the input starting from a specified index and returns them as a sequence.

fun main() {
    val pattern = Regex("ab.")
    val matches: Sequence<MatchResult> = pattern.findAll("abcabdeabf", 0)
    matches.forEach { match -> println(match.value) }
}

Output:

abc
abd

4. matches(): This function checks if the entire input string matches the regular expression.

fun main() {
    val pattern = Regex("g([ee]+)ks?")
    println(pattern.matches("geeks"))  // true
    println(pattern.matches("geeeeeeks"))  // true
    println(pattern.matches("geeksforgeeks"))  // false
}

Output:

true
true
false

5. matchEntire(): This function tries to match the entire input string to the regex pattern and returns the match if successful, otherwise returnsnull.

fun main() {
    val pattern = Regex("geeks?")
    println(pattern.matchEntire("geeks")?.value)  // geeks
    println(pattern.matchEntire("geeeeeks")?.value)  // null
}

Output:

geeks
null

6. replace(): This function replaces all occurrences of the pattern in the input string with a replacement string.

7.replaceFirst(): Replaces only the first occurrence.

fun main() {
    val pattern = Regex("xyz")
    println(pattern.replace("xyzxyz", "abc"))  // abcabc
    println(pattern.replaceFirst("xyzxyz", "abc"))  // abcxyz
}

Output:

abcabc
abcxyz

8. split(): This function splits the input string into parts based on the regular expression pattern.

fun main() {
    val pattern = Regex("\\s+")  // Split by whitespace
    val result = pattern.split("Kotlin is great")
    result.forEach { println(it) }
}

Output:

Kotlin
is
great
End of lesson.