In Scala, data validation and adding constraints to its usage is important for data processing. And checking if a regular expression is present in a string.
To check whether the strings contain a regular expression or regex we will use the findFirstIn() method.
Syntax:
regex.findfirstIn(string)
The method accepts the string in which regex is searched and returns an Option[String] value.
Program to check if a string contains a regular expression
Output
Explanation:
In this program, we have a string with is to be checked for regular expression which is created using the .r method. The method findFirstIn() is used to check for the presence of the pattern in the string which returns a none if no match is found and the matched string if a match is found.
need an explanation for this answer? contact us directly to get an explanation for this answer