• Latest Code...

    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")


    /* .....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


    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 ->
    println("$word matches")
    println("$word not matches")

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

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


    /**********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

    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


    /*....find only numbers.....

    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")


    /* ...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: 



    No comments:

    Post a Comment


    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace