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 :
"Beautiful Nature"
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) }
var str = "Beautiful Nature"
var x = Regex("\\AB")
\AB
println("The String ::Beautiful Nature:: starts with :: "+data)
The String ::Beautiful Nature:: starts with B
"Beautiful Nature"
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) }
The String ::Beautiful Nature:: starts with :: Bea
var x = Regex("\\bBea")
"Beautiful Nature"
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") } }
The String ::Beautiful Nature:: does not start with Bea
var x = Regex("\\BBea")
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") } }
var x = Regex("\\Bful")
The String ::Beautiful Nature:: does not start with ful
"Beautiful Nature"
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") } }
var x = Regex("\\d") Didn't find a match.
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") } }
str = "Beautiful Nature 92"
var x = Regex("\\d")
The String ::Beautiful Nature 92:: has a number in it
"Beautiful Nature"
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") } }
var x = Regex("\\D") Found a match.
"Beautiful Nature"
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") } }
var x = Regex("\\s") Found a match.
"Beautiful Nature"
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") } }
var str = "Beautiful"
var x = Regex("\\S")
The String ::Beautifu:: does not have a white space in it
"Beautiful Nature"
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") } }
var x = Regex("e\\Z")
The String ::Beautiful Nature:: ends with e