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




REGEX - METHODS


There are five methods in that we use commonly.


Let us see them in detail :

  1. scan() Method



    We have already seen the scan() Method in the earlier examples.

    The scan() Method is ued to search for a pattern and returns the result as a Array.

    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. Ruby finds them.

    Let us see in the below example,

    Example :



    str = "Cool Guy"
    x = str.scan(/u|o/)
    
    puts "The Array elements are : #{x}"
    
    if x
    	puts "Found a match"
    else
    	puts "Did not find a match"
    end
    


    Output :



      The Array elements are : ["o", "o", "u"]
      Found a match


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

    And we have used the scan() Method to find the results.

    x = str.scan(/u|o/)


    And Ruby checks the actual String,

    str = "Cool Guy"


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

    Now, if you see the output,

    The Array elements are : ["o", "o", "u"]


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

  2. match() Method



    match() Method returns a Match Data if the pattern is matched.

    Let us take the above example and understand the match() Method.

    Example :



    str = "Cool Guy"
    x = str.match("u|o")
    
    puts "The Match Data is : #{x}"
    
    if x
    	puts "Found a match"
    else
    	puts "Did not find a match"
    end
    


    Output :



      The Match Data is : o
      Found a match

  3. split() Method



    split() Method returns the searched results as a Array, 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 Array.

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

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

    Let us see in the below example:

    Example :



    str = "Hello Beautiful World"
    x = str.split("\s")
    
    puts "The Array elements are : #{x}"
    


    Output :



      The Array elements are : ["Hello", "Beautiful", "World"]


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

    x = str.split("\s")


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

    The Array elements are : ["Hello", "Beautiful", "World"]

    java_Collections

  4. sub() Method



    The sub() Method is used to replace the first occurrence of the searched pattern with a given text.

    Let us say, we have the below String,

    "Hello Beautiful World"


    And we want to replace the first white space with :.

    i.e. The new String should be,

    "Hello:Beautiful World"


    Let us see in the below example:

    Example :



    str = "Hello Beautiful World"
    x = str.sub("\s", ":")
    
    puts "The new String is : #{x}"
    


    Output :



      The new String is : Hello:Beautiful World


    So, in the above code, we have used the sub() Method to replace the first white space with :.

    x = re.sub("\s", ":")

    java_Collections


    Now, if you see the below output, we got the new String, where the first space got replaced by ':'.

    The new String is : Hello:Beautiful World

  5. gsub()



    The gsub() Method 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 :



    str = "Hello Beautiful World"
    x = str.gsub("\s", ":")
    
    puts "The new String is : #{x}"
    


    Output :



      The new String is : ('Hello:Beautiful:World', 2)


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

    x = str.gsub("\s", ":")