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




REGEX - SPECIAL SEQUENCES


Special Sequences in Regex are something that helps us write the patterns quite easy to write.


They begins with \ and are followed by a letter giving a special meaning to it.


Let us see them below :

  1. \A



    The \A is used to check if a String starts with a certain character or characters.

    We will be taking the same example to explain \A.

    Let us say, we have the 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 :



    fun main(args: Array) {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\AB")
    
    	val match = x.find(str)
    
    	var data = match?.value
    
    	println("The String ::Beautiful Nature:: starts with :: "+data)
    }
    


    Output :



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


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

    var str = "Beautiful Nature"

    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 function findall().

    var x = Regex("\\AB")


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

    \AB


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

    println("The String ::Beautiful Nature:: starts with :: "+data)


    And prints the value,

    The String ::Beautiful Nature:: starts with B

  2. \b



    Well! \b is for Backspace in Kotlin.

    But! Hold On!

    In this case \b is a Special Sequence and is not a backspace.

    So, the Special Sequence \b is used to check if a String starts or ends with a certain character or characters.

    We will be taking the same example to explain \b.

    Let us say, we have the 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 :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\bBea")
    
    	val match = x.find(str)
    
    	var data = match?.value
    
    	println("The String ::Beautiful Nature:: starts with :: "+data)
    }
    
    


    Output :



      The String ::Beautiful Nature:: does not start with :: Bea


    The output is a little weird. Even though the String Beautiful Nature starts with Bea.

    Still we got the output,

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


    That is because the pattern in the below statement,

    var x = Regex("\\bBea")


    Is a little confusing for Kotlin. Because \b (i.e. In \bBea) could be backspace as well.

    So, we get the above output.

  3. \B



    We have already seen that the lower case b i.e. \b, is used to check if a String begins or ends with a certain character or characters.

    Well! The upper case B i.e. \B is just the opposite of it.

    i.e. \B, is used to check if a String does not begins or ends with a certain character or characters.

    Sounds complex?

    Let us clear with the below example..

    Let us say, we have the 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 :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\BBea")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: starts with Bea")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not start with Bea")
    	}
    }
    


    Output :



      The String ::Beautiful Nature:: does not start with Bea


    The output is a little weird. Even though the String Beautiful Nature starts with Bea.

    Still we got the output,

    The String ::Beautiful Nature:: does not start with Bea


    That is because the pattern in the below statement,

    var x = Regex("\\BBea")


    Is used to check if the String i.e. Beautiful Nature does not start with Bea.

    And in this case it starts with Bea. So, there is a mismatch.

    So, we get the above output.

    Let's fix the above code in the next example.

    Example :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\Bful")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: starts with ful")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not start with ful")
    	}
    }
    


    Output :



      The String ::Beautiful Nature:: does not start with ful


    And all we have done is, put some other set of characters i.e. ful, with which the String Beautiful Nature doesn't starts or ends.

    var x = Regex("\\Bful")


    And this time we got the right output.

    The String ::Beautiful Nature:: does not start with ful

  4. \d



    \d, is used to check if a String contains at least one number.

    Let us clear with the below example..

    Let us say, we have the String,

    "Beautiful Nature"


    And let us check if the above String has any number or not? Well! We know it doesn't.

    Let us see that in the below example,

    Example :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\d")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: has a number in it")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not have a number in it")
    	}
    }
    


    Output :



      The String ::Beautiful Nature:: does not have a number in it


    And the output is obvious.

    Since, the above String Beautiful Nature has no numbers in it. The expression,

    var x = Regex("\\d")
    
    Didn't find a match.


    Let's fix the above code in the next example.

    Example :



    fun main() {
    
    	var str = "Beautiful Nature 92"
    	var x = Regex("\\d")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature 92:: has a number in it")
    	}
    	else {
    		println("The String ::Beautiful Nature 92:: does not have a number in it")
    	}
    }
    


    Output :



      The String ::Beautiful Nature 92:: has a number in it


    And all we have done is, modified the String to Beautiful Nature 92.

    str = "Beautiful Nature 92"


    And this time, the String has a number in it. So the below expression,

    var x = Regex("\\d")


    Finds a match,

    And we get the below output,

    The String ::Beautiful Nature 92:: has a number in it

  5. \D



    \D, is the opposite of \d is used to check that the String should not contain any number.

    Let us clear with the below example..

    Let us say, we have the String,

    "Beautiful Nature"


    Which doesn't have any numbers in it

    Let us see that in the below example,

    Example :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\D")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: has a number in it")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not have a number in it")
    	}
    }
    


    Output :



      The String ::Beautiful Nature:: does not have any number in it


    And the output is obvious.

    Since, the above String Beautiful Nature has no numbers in it. The expression,

    var x = Regex("\\D")
    
    Found a match.

  6. \s



    \s, is used to check if the String has a white space in it.

    Let us clear with the below example..

    Let us say, we have the String,

    "Beautiful Nature"


    And there is a white space between Beautiful and Nature. So \s would return true.

    Let us see that in the below example,

    Example :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("\\s")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: has a white space in it")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not have a white space in it")
    	}
    }
    


    Output :



      The String ::Beautiful Nature:: has a white space in it


    And the output is obvious.

    Since, the above String Beautiful Nature has a white space between Beautiful and Nature. The expression,

    var x = Regex("\\s")
    
    Found a match.

  7. \S



    \S is the opposite of \s and is used to check if there is no white space in the String.

    Let us clear with the below example..

    Let us say, we have the String,

    "Beautiful Nature"


    And there is a white space between Beautiful and Nature. So \S would return false.

    Let us see that in the below example,

    Example :



    fun main() {
    
    	var str = "Beautiful"
    	var x = Regex("\\S")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: has a white space in it")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not have a white space in it")
    	}
    }
    


    Output :



      The String ::Beautifu:: does not have a white space in it


    Even this time the output is obvious.

    We have used the String Beautiful this time. And there is no white space in it.

    var str = "Beautiful"


    So, the expression,

    var x = Regex("\\S")


    Finds a match because there is no white space in the String.

    And we get the below output,

    The String ::Beautifu:: does not have a white space in it

  8. \Z



    \Z 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 :



    fun main() {
    
    	var str = "Beautiful Nature"
    	var x = Regex("e\\Z")
    
    	var result = x.containsMatchIn(str)
    
    	if (result) {
    		println("The String ::Beautiful Nature:: ends with e")
    	}
    	else {
    		println("The String ::Beautiful Nature:: does not end with e")
    	}
    }
    


    Output :



      The String ::Beautiful Nature:: ends with e


    So, in the above example, we have checked, if the above String ends with the letter e.

    var x = Regex("e\\Z")


    And since, the String Beautiful Nature ends with e. We got the below output.

    The String ::Beautiful Nature:: ends with e