I am beginner in Scala and I was wondering how I can build a function to check if it matches a definite pattern or not?
For example:
def patternFound(s:String): Boolean = (s) match {
case s matches xyxy pattern => true //where x,y are two consecutive characters in the string
case s matches xxyy pattern => false //where x, y are two characters in that string
case (_) => false //default
}
//Here x,y are not definite characters but the string s should match a pattern
//which consist a string of pattern containing characters in alternating positions
patternFound("babab")//true because pattern of xyxy found in it
patternFound("baabba")//false because pattern of xxyy found in it
Can anyone show with an example how I can achieve this?
Looking for a solution which returns true for any occurrence of xyxyxy pattern in a string, but returns false when the pattern is xxyy in that string.
Example: The function should return true if the string is "babab" or "ababa" (which has pattern xyxy in it), but returns false for "aabba" or "bbaab" (which has the pattern xxyy in it)
Any help is appreciated! Thank you in advance.