Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




REGEX - FUNCTIONS


There are five functions in that we use commonly.


Let us see them in detail :

  1. findall() Function



    We have already seen the findall() Function in the earlier examples.

    The findall() Function is ued to search for a pattern and returns the result as a List.

    Say for example, let us take the String,

    "Cool Guy"


    And let us say, you want to check, how many times o and u is there in the string?

    In such case | Symbol is the option. Just give the option of u|o(i.e. u or o).

    'u|o'


    And no matter, where u or o is. Kotlin finds them.

    Let us see in the below example,

    Example :



    fun main() {
    
    	var str = "Cool Guy"
    	var x = Regex("u|o")
    
    	val match = x.findAll(str)
    
    	match.forEach { word -> println("The string has : "+word.value) }
    
    	if (x.containsMatchIn(str)) {
    		println("Found a match")
    	}
    	else {
    		println("Did not find a match")
    	}
    }
    


    Output :



      The string has : o
      The string has : o
      The string has : u
      Found a match


    So, in the above example, we have checked, how many times o and u is there in the string?

    So, at first, we have declared the Regex expression,

    var x = Regex("u|o")


    And we have used the findall() Function to find the results.

    val match = x.findAll(str)


    And Kotlin checks the actual String,

    var str = "Cool Guy"


    And finds that there is a word, Cool has two os and the word, Guy has one u.

    Now, if you see the output,

    The string has : o
    The string has : o
    The string has : u


    So, the findall() Function had returned the Array str that has three items in it. i.e. o, o and u.
    java_Collections

  2. find() Function



    find() Function returns a Match Result if the pattern is matched.

    Let us take the above example and understand the search() Function.

    Example :



    fun main() {
    
    	var str = "Cool Guy"
    	var x = Regex("u|o")
    
    	val match = x.find(str)
    
    	println("The Match Result is : "+match)
    
    	if (x.containsMatchIn(str)) {
    		println("Found a match")
    	}
    	else {
    		println("Did not find a match")
    	}
    }
    


    Output :



      The Match Result is : kotlin.text.MatcherMatchResult@6cd8737
      Found a match


    So, if you see the above output, this is how a Match Result looks like.

    kotlin.text.MatcherMatchResult@6cd8737


    Just remember, a Match Result is an object that has the all the necessary information about the searched results.

    But if we see if it did return the match or not. We can find that there is a match field that returned the character o.

    match='o'


    Hey! Wait! How did we come to know, match has o in it. Well! We will see it later.

    But what about the other o and U.

    Well! the find() Function would just search for the first occurrence of the match. And the rest two matches would be discarded.

    Now, let us see, how we came to know match has o in it.

    Example :



    fun main() {
    
    	var str = "Cool Guy"
    	var x = Regex("u|o")
    
    	val match = x.find(str)
    
    	if (match != null) {
    		println("The Match Result is : "+match.value)
    	}
    
    	if (x.containsMatchIn(str)) {
    		println("Found a match")
    	}
    	else {
    		println("Did not find a match")
    	}
    }
    


    Output :



      The Match Result is : o
      Found a match


    So, we have used the find() Function to get the Match Result(As we have seen find() Function returns a Match Object).

    val match = x.find(str)


    Then we have used the value attribute of Match Result(i.e. match.result) to get the actual value of Match Result,

    println("The Match Result is : "+match.value)

  3. split() Function



    split() Function returns the searched results as a List, splitted by the search pattern.

    Let us say, we have the below String,

    "Hello Beautiful World"


    And we want to split the above String with respect to white space and put the pieces inti a list.

    i.e. There are two spaces between Hello and Beautiful, Beautiful and World.

    So what we want is, form a List with Hello, Beautiful and World.

    Let us see in the below example:

    Example :



    fun main() {
    
    	var str = "Hello Beautiful World"
    	var x = Regex("\\s")
    
    	val match = x.split(str)
    
    	match.forEach { word -> println("The string has : "+word) }
    }
    


    Output :



      The string has : Hello
      The string has : Beautiful
      The string has : World


    So, in the above code, we have used the split() Function to split the String Hello Beautiful World using a white space (i.e. \s).

    So, we have the Regex,

    var x = Regex("\\s")


    Then we have used the split() Function,

    val match = x.split(str)


    Next, we have used forEach to iterate the Array,

    match.forEach { word -> println("The string has : "+word) }


    Now, if you see the below output, we got a List with Hello, Beautiful and World.

    The string has : Hello
    The string has : Beautiful
    The string has : World

    java_Collections

  4. replace() Function



    The replace() Function is used to replace the searched pattern with a given text.

    Let us say, we have the below String,

    "Hello Beautiful World"


    And we want to replace white spaces with :.

    i.e. The new String should be,

    "Hello:Beautiful:World"


    Let us see in the below example:

    Example :



    fun main() {
    
    	var str = "Hello Beautiful World"
    	var x = Regex("\\s")
    
    	val match = x.replace(str, ":")
    
    	println("The new String is : "+match)
    }
    


    Output :



      The new String is : Hello:Beautiful:World


    So, in the above code, we have used the replace() Function to replace white spaces with :.

    So, to detect white spaces, we have the Regex,

    var x = Regex("\\s")


    Then we have used the replace() function to replace space with :

    val match = x.replace(str, ":")


    Now, if you see the below output, we got the new String, where the spaces are replaced by :.

    The new String is : Hello:Beautiful:World