• Latest Code...

    Featured Post

    Implementing Hilt in a Kotlin Android Jetpack Compose Project with MVVM Architecture

     In modern Android development, maintaining a scalable codebase can be challenging, especially when it comes to dependency management. Hilt,...

    Regular Expression Example in KOTLIN - Check EMAIL address and more

     


    Represents a compiled regular expression. Provides functions to match strings in text with a pattern, replace the found occurrences and split text around matches.

    Here are some examples;


    1. Check Email address valid or not (using matches )

    println(".....email pattern check....")
    val emails = listOf("abc_d@gmail.com", "test!@hotmail.com","abc_d@mailcom",
    "343434ffsdkfs#mail", "p6060606@gmail.com","_testmaail$@last.com")

    val emailPattern = "[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\.[a-zA-Z.]{2,18}".toRegex()

    emails.forEach { email ->
    if (emailPattern.matches(email)) {
    println("$email matches")
    } else {
    println("$email does not match")
    }
    }

    output:

    /* .....email pattern check....
    abc_d@gmail.com matches
    test!@hotmail.com does not match
    abc_d@mailcom does not match
    343434ffsdkfs#mail does not match
    p6060606@gmail.com matches
    _testmaail$@last.com does not match
    */

    2.  Find the exact substring(partially word) inside the words in list

     - containsMatchIn

    matches

    val testWords = listOf("test", "testified", "testing", "techtest",
    "exam","labtest", "roadtest", "protest", "tstom", "testimonials")

    val pattern = "test".toRegex()

    println("*********particular sub string inside the string************")
    println("containsMatchIn function")

    testWords.forEach{ word ->
    if(pattern.containsMatchIn(word)){
    println("$word matches")
    }else{
    println("$word not matches")
    }
    }



    println("********* exact string to another string ************")
    println("matches function")

    testWords.forEach { word ->
    if (pattern.matches(word)) {
    println("$word matches")
    }else{
    println("$word not matches")
    }
    }

    output

    /**********particular sub string inside the string************
    containsMatchIn function
    test matches
    testified matches
    testing matches
    techtest matches
    exam not matches
    labtest matches
    roadtest matches
    protest matches
    tstom not matches
    testimonials matches
    ********* exact string to another string ************
    matches function
    test matches
    testified not matches
    testing not matches
    techtest not matches
    exam not matches
    labtest not matches
    roadtest not matches
    protest not matches
    tstom not matches
    testimonials not matches */

    3. Find only only from the string or sentence - findAll

    findAll
    println("....find only numbers.....")
    val findNumInText = "Use numerals, however, when the number modifies a unit of measure, time, proportion, etc.: 2 inches, 5-minute delay, 65 mph, 23 years old, page 23, 2 percent. "

    val patternN = "\\d+".toRegex()
    val founds = patternN.findAll(findNumInText)

    founds.forEach { num ->
    val foundValue = num.value
    println(foundValue)
    }

    output

    /*....find only numbers.....
    2
    5
    65
    23
    23
    2*/

    4. Find particular word and its indices in sentence 

    println("...Find particular word and its indices in sentence...using\\\\b operator......")
    val sent = "But you never know now do you now do you now do you."
    val patternIs = "\\byou\\b".toRegex()
    val matchesIS = patternIs.findAll(sent)
    matchesIS.forEach { word ->
    val value = word.value
    val index = word.range
    println("$value found at indexes: $index")
    }

    output:

    /* ...Find particular word and its indices in sentence...using\\b operator......
    you found at indexes: 4..6
    you found at indexes: 26..28
    you found at indexes: 37..39
    you found at indexes: 48..50
    */


    If you want know more about regex, please check out this: 

    https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/

    https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html



    Contact Form

    Name

    Email *

    Message *