Kotlin String matches

The matches function in Kotlin is used to check if an entire string matches a specified regular expression. This function belongs to the String class in the Kotlin standard library and provides a straightforward way to perform pattern matching on a string.

Table of Contents

  1. Introduction
  2. matches Function Syntax
  3. Understanding matches
  4. Examples
    • Basic Usage
    • Checking for a Valid Email Address
    • Matching a Specific Pattern
  5. Real-World Use Case
  6. Conclusion

Introduction

The matches function evaluates whether the entire string conforms to a given regular expression pattern. This is useful for validating input against specific patterns, such as email addresses, phone numbers, or custom formats.

matches Function Syntax

The syntax for the matches function is as follows:

fun CharSequence.matches(regex: Regex): Boolean

Parameters:

  • regex: The regular expression pattern to match against the string.

Returns:

  • true if the entire string matches the specified regular expression, false otherwise.

Understanding matches

The matches function checks if the entire string conforms to the given regular expression pattern. If any part of the string does not match the pattern, the function returns false.

Examples

Basic Usage

To demonstrate the basic usage of matches, we will check if a string matches a simple pattern.

Example

fun main() {
    val text = "12345"
    val pattern = "\\d{5}".toRegex()
    val isMatch = text.matches(pattern)
    println("Does the text match the pattern? $isMatch")
}

Output:

Does the text match the pattern? true

Checking for a Valid Email Address

This example shows how to use matches to validate if a string is a valid email address.

Example

fun main() {
    val email = "user@example.com"
    val emailPattern = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+
quot;.toRegex()
    val isEmailValid = email.matches(emailPattern)
    println("Is the email valid? $isEmailValid")
}

Output:

Is the email valid? true

Matching a Specific Pattern

This example demonstrates how to use matches to check if a string matches a specific custom pattern.

Example

fun main() {
    val text = "Kotlin123"
    val pattern = "^[A-Za-z]+\\d+
quot;.toRegex() // Letters followed by digits
    val isMatch = text.matches(pattern)
    println("Does the text match the pattern? $isMatch")
}

Output:

Does the text match the pattern? true

Real-World Use Case

Validating User Input

In real-world applications, the matches function can be used to validate user input against predefined patterns, such as phone numbers or postal codes.

Example

fun main() {
    val phoneNumber = "+123-456-7890"
    val phonePattern = "^\\+[0-9]{1,3}-[0-9]{3}-[0-9]{4}
quot;.toRegex()
    val isPhoneNumberValid = phoneNumber.matches(phonePattern)

    println("Is the phone number valid? $isPhoneNumberValid")
}

Output:

Is the phone number valid? true

Conclusion

The matches function in Kotlin's String class is a powerful method for validating strings against regular expression patterns. It provides a straightforward way to ensure that strings conform to specific formats, making it useful for various applications, including input validation and pattern matching. 

By understanding and using this function, you can effectively manage string validation and pattern matching in your Kotlin applications.

Comments