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




REGEX - META-CHARACTERS


Meta-Characters are the characters that is used by Regex to treat it in a different way.


So, we will be taking the String, Beautiful Nature,

java_Collections

And use all the Meta-Characters, to search for different patterns in the String.


Let us look at the different Meta-Characters, we can use :

  1. '^' Symbol



    The ^ symbol is used to check if a String starts with a certain character or characters.

    Say for example, let us take the above String,

    "Beautiful Nature"


    And let us check if the above String, starts with the letter B or Bea.

    Let us see in the below example,

    Example :



    str = "Beautiful Nature"
    x = str.match("^B")
    
    if x
    	puts "The String ::Beautiful Nature:: starts with B"
    else
    	puts "The String ::Beautiful Nature:: does not start with B"
    end
    	
    y = str.match("^Bea")
    
    if y
    	puts "The String ::Beautiful Nature:: starts with Bea"
    else
    	puts "The String ::Beautiful Nature:: does not start with Bea"
    end
    


    Output :



      The String ::Beautiful Nature:: starts with B
      The String ::Beautiful Nature:: starts with Bea


    So, in the above example, we have taken the String Beautiful Nature and initialised it to a variable str.

    x = str.match("^B")

    java_Collections


    So, at first we have checked, if the above String starts with the letter B.

    And this is where we have used the Method match().

    x = str.match("^B")


    So, the match() Method accepts two arguments, the pattern and the actual String.
    java_Collections


    Now, let us come to the ^ Symbol, which is used in the pattern,

    ^B


    That actually says, "Check if the String starts with the letter B".

    And in this case the String starts with B. So, the control enters the blocks of if statement,

    if x
    	puts "The String ::Beautiful Nature:: starts with B"


    And prints the value,

    The String ::Beautiful Nature:: starts with B


    Similarly, we have used the ^ Symbol in the next line,

    y = str.match("^Bea")


    To check, if the String begins with Bea.

    ^Bea


    And in this case, the String begins with Bea. So, the control enters the blocks of if,

    if y
    	puts "The String ::Beautiful Nature:: starts with Bea"


    And the below output is printed on the screen,

    The String ::Beautiful Nature:: starts with Bea




    Example Description
    str = "Beautiful Nature"

    x = str.match("^e")
    There is no match as the String doesn't

    begin with 'e'. But it begins with 'B'
    str = "Beautiful Nature"

    x = str.match("^Bae")
    There is no match as the String doesn't

    begin with 'Bae'.

  2. '$' Symbol



    The $ symbol is used to check if a String ends with a certain character or characters.

    Say for example, let us take the above String,

    "Beautiful Nature"


    And let us check if the above String, ends with the letter e or ture.

    Let us see in the below example,

    Example :



    str = "Beautiful Nature"
    x = str.match("e$")
    
    if x
    	puts "The String ::Beautiful Nature:: ends with e"
    else
    	puts "The String ::Beautiful Nature:: does not end with e"
    end
    	
    y = str.match("ture$")
    
    if y
    	puts "The String ::Beautiful Nature:: ends with ture"
    else
    	puts "The String ::Beautiful Nature:: does not end with ture"
    end
    


    Output :



      The String ::Beautiful Nature:: ends with e
      The String ::Beautiful Nature:: ends with ture


    So, in the above example, we have taken the String Beautiful Nature and initialised it to a variable str.

    str = "Beautiful Nature"


    So, at first we have checked, if the above String ends with the letter e.

    x = str.match("e$")


    That actually says, "Check if the String ends with the letter e".

    And in this case the String ends with e. So, the control enters the blocks of if statement,

    if x
    	puts "The String ::Beautiful Nature:: ends with e"


    And prints the value,

    The String ::Beautiful Nature:: ends with e


    Similarly, we have used the ^ Symbol in the next line,

    y = str.match("ture$")


    To check, if the String ends with ture.

    ture$


    And in this case, the String ends with ture. So, the control enters the blocks of if,

    if y
    	puts "The String ::Beautiful Nature:: ends with ture"


    And the below output is printed on the screen,

    The String ::Beautiful Nature:: ends with ture




    Example Description
    str = "Beautiful Nature"

    x = str.match("B$")
    There is no match as the String doesn't

    end with 'B'. But it ends with 'e'
    str = "Beautiful Nature"

    x = str.match("tur$")
    There is no match as the String doesn't

    ends with 'tur'.

  3. '.' Symbol



    The . symbol is used to check if a String matches any character except a new line.

    Say for example, let us take the above String,

    "Beautiful Nature"


    And let us say, you know there is a String but you forgot the spelling of Beautiful. You can remember it is, Beaut and it ends with an l.

    In such case . operator comes to rescue. Just substitute the unknown characters with .. And you can search for the actual String.

    'Beaut...l'


    The above . dots will be replaced by the actual value.

    Let us see in the below example,

    Example :



    str = "Beautiful Nature"
    x = str.match("Beaut...l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is : Beautiful


    So, in the above example, we have taken the String Beautiful Nature and initialised it to a variable str.

    str = "Beautiful Nature"


    And checked, if at all there is a word, that starts with Beaut and ends with an l.

    x = str.match("Beaut...l")


    And Ruby checks the actual String,
    java_Collections


    And finds that there is a word, Beautiful that starts with Beaut and ends with an l. And the three dots '.' could be replaced by i, f and u.
    java_Collections


    So, the word Beautiful is fetched and put to the variable x.

    x = str.match("Beaut...l")

    java_Collections

    Note : You have to put the exact number of dots . as the number of characters, else the pattern won't return the exact String.


    And the print statement,

    puts "The string is :"


    Prints the List,

    The string is : Beautiful




    Example Description
    str = "Hello"

    x = x = str.match(".....")
    There is a match as the String has a 5 letter word 'Hello' that matches with the 5 '.'s
    str = "Beautiful"

    x = x = str.match(".....")
    There is no match as the String 'Beautiful' is a 9 letter word that doesn't match 5 '.'s

  4. '+' Symbol



    The + is used to check one or more occurrences of a pattern.

    Let us take a different String to understand the + Symbol,

    "Cool Guy"


    And let us say, you know there is a String but you forgot the spelling of Cool. And you are confused, if there is one o or two os in the string Cool.

    In such case + Symbol comes to rescue. Just place the + symbol after o.

    'Co+l'


    And no matter, how many 'o's are there. The exact String would be returned.

    Let us see in the below example,

    Example :



    str = "Cool Guy"
    x = str.match("Co+l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is : Cool


    So, in the above example, we have checked, if at all there is a word, that starts with Co and ends with an l. No matter how many os are there.

    x = str.match("Co+l")


    And Ruby checks the actual String,

    And finds that there is a word, Cool that starts with Co and ends with an l. And there is one o that can be substituted.

    So, the word Cool is fetched and put to the variable x.

    x = str.match("Co+l")

    java_Collections


    And the print statement,

    puts "The string is : #{x}"


    Prints the value of x as List,

    The string is : ['Cool']


    Even if there were multiple 'o's in the String. The match would be found.

    Example :



    str = "Cooooool Guy"
    x = str.match("Co+l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is : Cooooool




    Example Description
    str = "Heo" There is no match as the String 'Heo'
    x = str.match("Hel+o") doesn't have an 'l' in it.

  5. '*' Symbol



    The * is same as + Symbol with a mild difference. It is used to check zero or more occurrences of a pattern.

    Let us take the below String to understand the * Symbol,

    "Cool Guy"


    And let us say, you know there is a String but you forgot the spelling of Cool. And you are confused, if there is one o or two os in the string Cool.

    In such case * Symbol comes to rescue. Just place the * symbol after o.

    'Co*l'


    And no matter, how many os are present. The exact String would be returned.

    Let us see in the below example,

    Example :



    str = "Cool Guy"
    x = str.match("Co*l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is : Cool


    So, in the above code, we have checked, if at all there is a word, that starts with Co and ends with an l. No matter how many os are there.

    x = str.match("Co*l")


    And Ruby checks the actual String,

    And finds that there is a word, Cool that starts with Co and ends with an l. And there is one o that can be substituted.

    So, the word Cool is fetched and put to the variable x.

    x = str.match("Co*l")

    java_Collections


    And the print statement,

    puts "The string is : #{x}"


    Prints the value of x as List,

    The string is : Cool


    So, how is * Symbol different from + Symbol?

    Let us consider the String where there is no os at all in the String.

    "Cl Guy"


    Example :



    str = "Cl Guy"
    x = str.match("Co*l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is : Cl


    That is because the * Symbol searches for zero or more occurrences.

    So, the String,

    str = "Cl Guy"


    Doesn't have any os in it. Still the pattern,

    x = str.match("Co*l")


    Is able to search the String Cl.



    Example Description
    str = "Heleo"

    x = str.match("Hel*o")
    There is no match as the String 'Heleo' has an 'e' between 'l' and 'o'.
    str = "Helo"

    x = str.match("Hel*o")
    There is a match as the String 'Helo' has one 'l' in it.

  6. '?' Symbol



    The ? is same as + and * Symbol with a mild difference. It is used to check zero or exactly one occurrences in a pattern.

    Let us take the below String to understand the ? Symbol,

    "Cool Guy"


    So, at first, let us understand, how is ? Symbol different from + and * Symbol.

    And as we have done for the * Symbol, you know there is a String but you forgot the spelling of Cool.

    And you are confused, if there is one o or two os in the string Cool.

    Let's see, how ? symbol responds in such case.

    'Co?l'


    Let us see in the below example,

    Example :



    str = "Cool Guy"
    x = str.match("Co?l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is :


    And we got an empty String.

    This is because the below line,

    x = str.match("Co?l")


    Searches for the pattern Co?l.

    And ? Symbol searches for zero or one occurrence of o(i.e. Co?l). But in the String Cool Guy, The substring Cool has two os. So it returns nothing.

    So, when should ? Symbol find a match.

    Let us consider the String where there is no os or 1 o in the String.

    "Cl Guy"


    Or

    "Col Guy"


    Example :



    str = "Cl Guy"
    x = str.match("Co?l")
    
    puts "The string is : #{x}"
    


    Output :



      The string is : Cl




    Example Description
    str = "Heleo"

    x = str.match("Hel?o")
    There is no match as the String 'Heleo' has an 'e' between 'l' and 'o'.
    str = "Hello"

    x = str.match("Hel?o")
    There is no match as the String 'Hello' has two 'l's in it.

  7. '|' Symbol



    The | is like OR.

    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 result is : #{x}"
    


    Output :



      The result is : ["o", "o", "u"]


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

    And this time we have used the scan() method because we will be getting multiple results as an Array.

    The syntax is a little different. Instead of double quotes "". We would be using slash //.

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

    java_Collections


    And Ruby checks the actual String,

    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 result is : ["o", "o", "u"]


    The Array x has three items in it. i.e. o, o and u.

    We can also search for a String using | Symbol.

    Say for example, you forgot if the String is Cool Guy or Fool Guy.

    In that case we can use the | operator.

    Example :



    str = "Cool Guy"
    x = str.scan(/Cool|Fool/)
    
    puts "The string is : #{x}"
    


    Output :



      The string is : ['Cool']


    So, in the above code we wanted to check, if the String Cool Guy.

    str = "Cool Guy"


    Contains the word, Cool or Fool.

    x = str.scan(/Cool|Fool/)


    And since, there is just one match for Cool. The variable x would have one item in the Array i.e. Cool.



    Example Description
    str = "Heleo"

    x = str.scan(/P|u/)
    There is no match as the String 'Heleo' has no 'P' or 'u' in it.

  8. '()' Symbol



    The () is used to group Substrings.

    Say for example, let us take a different String,

    "Cool Owl"


    Now, let us say, you want to check, how many times the letters w or o is followed by the letter l.

    In such case () Symbol is the option.

    Let us see in the below example,

    Example :



    str = "Cool Owl"
    x = str.scan(/(w|o)l/)
    
    puts "The elements in the Array are : #{x}"
    


    Output :



      The elements in the Array are : [["o"], ["w"]]


    So, in the above example, we have checked, how many times the letters w or o is followed by the letter l.

    x = str.scan(/(w|o)l/)


    And we have placed the letters w and o inside the () Symbol. Also if you note, we have used the | (i.e. or) Symbol.

    Now, Ruby checks the actual String, and tries to find a match where the the letters w or o is followed by the letter l.

    (w|o)l


    And finds that there is are two words, Cool and Owl.

    Where o is followed by l in Cool. And w is followed by l in Owl.

    Now, if you see the output,

    The elements in the Array are : [["o"], ["w"]]


    The Array x has two items in it. i.e. o and w. First element o is the match for Cool. And the second match w is the match for Owl.



    Example Description
    str = "Hello Mellow Howl"

    x = str.scan(/(l|H)o/)
    There is 3 matches as the String 'Hello Mellow Howl' has 'lo', 'lo' & 'Ho'.
    str = "Hello"

    x = str.scan(/(H|e)o/)
    There is no match as the String 'Hello' has no match for 'Ho' or 'eo'.

  9. '{}' Symbol



    The {} is used to match the exact number of occurrences of a letter in a String.

    Say for example, let us take the String,

    "Cool Guy"


    Now, let us say, you want to check, if the letter C is followed by two os.

    In such case {} Symbol is the option.

    Let us see in the below example,

    Example :



    str = "Cool Guy"
    x = str.scan(/Co{2}/)
    
    puts "The element in the Array is : #{x}"
    


    Output :



      The element in the Array is : ["Coo"]


    So, in the above example, we have checked, if the letter C is followed by two os.

    x = str.scan(/Co{2}/)


    And we have placed the number 2 inside the {} Symbol.

    Now, Ruby checks the actual String, and tries to find a match where the letter C is followed by two os.

    Co{2}


    And finds that there is are one match, Coo.

    Now, if you see the output,

    The element in the Array is : ["Coo"]


    There is just one match i.e. Coo.

    Next, let us take another String.

    "Cool owl"


    Now, let us say, you want to check, if the String has one o or two os.

    In such case, we will use the {} Symbol with two numbers.

    Let us see in the below example,

    Example :



    str = "Cool owl"
    x = str.scan(/o{1,2}/)
    
    puts "The elements in the Array are : #{x}"
    


    Output :



      The elements in the Array are : ["oo", "o"]


    So, in the above example, we have checked, if the String,

    str = "Cool owl"


    Has one o or two os.

    x = str.scan(/o{1,2}/)


    And we have placed the number 1,2 inside the {} Symbol(i.e. o{1,2}).

    Now, Ruby checks the actual String, and tries to find a match where the letter o is present once or twice.

    o{1,2}


    Now, if you see the output,

    The elements in the Array are : ["oo", "o"]


    The Array x has two items in it. i.e. oo and o. First element oo is the match for Cool. And the second match o is the match for owl.

  10. 0. '[]' Symbol



    The [] is used to match a set of characters in a String.

    Say for example, let us take the String,

    "Cool Guy"


    Now, let us say, you want to check, if the letters o, G, z and y are present in the String or not.

    In such case [] Symbol is the option.

    Let us see in the below example,

    Example :



    str = "Cool Guy"
    x = str.scan(/[oGzy]/)
    
    puts "The elements in the Array are : #{x}"
    


    Output :



      The elements in the Array are : ["o", "o", "G", "y"]


    So, in the above example, we have checked, if the letters o, G, z and y are present in the String or not.

    x = str.scan(/[oGzy]/)


    And we have placed all the letters o, G, z and y inside the [] Symbol.

    Now, Ruby checks the actual String, and tries to find a match for all the letters o, G, z and y inside the []

    [oGzy]


    And finds that there is are four matches.

    Now, if you see the output,

    The elements in the Array are : ["o", "o", "G", "y"]


    You can find that there are four matches. i.e. Two match for o, and one match for G and y. But no match for z as z is not present in the String.

    Use of '-' with '[]'



    We can use - with the [] symbol. Which is short form of to.

    The below pattern:

    [a-f]


    Says, search for all the characters from a to f(i.e. a, b, c, d, e and f).

    Similarly, the pattern :

    [0 - 100]


    Says, to search for all the numbers from 0 to 100.

    Also, the pattern :

    [0-9][0-9]


    Says, to search all the numbers from 00 to 99

    Use of '^' with '[]'



    We can also use ^ with the [] symbol. Which is short form of not.

    The below pattern:

    [^oGzy]


    Says, search for all the characters except the letters o, G, z and y.

    Example :



    str = "Cool Guy"
    x = str.scan(/[^oGzy]/)
    
    puts "The elements in the Array are : #{x}"
    


    Output :



      The elements in the Array are : ["C", "l", " ", "u"]


    So, if you see the above output, only the letters o, G, z and y are excluded from the String Cool Guy.

  11. 1. '\' Symbol



    \ Symbol is said to be an escape character.

    Say for example, you have a sentence,

    How are you?


    And you are supposed to search if there is a question mark(i.e. ?) after the substring you(i.e. you?)

    But as we have seen ? is a Meta-Character.

    Now, if you don't want ? to behave as a Meta-Character. You can use \ Symbol before it.

    Let us see in the below example :

    At first let us see, what happens if you don't put the escape symbol \

    Example :



    str = "How are you?"
    x = str.scan(/you?$/)
    
    puts "The elements in the Array are : #{x}"
    


    Output :



      The elements in the Array are : []


    So, all we have done is, checked if the String How are you?.

    str = "How are you?"


    Ends with you?.

    x = str.scan(/you?$/)


    But if you see the output, we got an empty Array.

    The elements in the Array are : []


    That is because ? is a Meta-Character. So, it gets confused and returns nothing.

    Let's correct it in the below example.

    Example :



    str = "How are you?"
    x = str.scan(/you\?$/)
    
    puts "The elements in the Array are : #{x}"
    


    Output :



      The elements in the Array are : ["you?"]


    And as we can see, we have just placed the escape symbol \ before ?.

    x = str.scan(/you\?$/)


    So that ? is not treated as a Meta-Character.

    And we got the desired output.

    The elements in the Array are : ["you?"]