Regex 对于给定的字符串,检查是否与模式[scala]匹配

Regex 对于给定的字符串,检查是否与模式[scala]匹配,regex,scala,functional-programming,pattern-matching,Regex,Scala,Functional Programming,Pattern Matching,我是Scala的初学者,我想知道如何构建一个函数来检查它是否符合特定的模式 例如: 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 a

我是Scala的初学者,我想知道如何构建一个函数来检查它是否符合特定的模式

例如:

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
有人能举例说明我是如何做到这一点的吗?

正在寻找一种解决方案,该解决方案对于字符串中出现的任何XY模式都返回true,但当该字符串中的模式为xxyy时返回false

示例:如果字符串为“babab”或 “ababa”(其中包含模式XY),但为“aabba”返回false 或“bbaab”(其中包含xxyy图案)

感谢您的帮助!提前谢谢。

语法不正确。 您需要从函数体中删除“s匹配项”,它已经在方法定义行(s)匹配项中


另请参见

正则表达式和look arounds可能实现这一点,但我刚刚创建了一个helper函数:

/**
  * Checks for recurring pattern in a String
  * @param s The input String to check
  * @param patternSize The size of the expected pattern.  For example in the String "aabbaabbaabb" the pattern is "aabb" which is a length of 4
  */
def checkPattern(s: String, patternSize: Int): Boolean = {
  val grouped = s.grouped(patternSize)
  grouped.toSet.size == 1 // everything should be the same
}
checkPattern("abababab", 2) // true
checkPattern("aabbaabbaabb", 2) // false
checkPattern("aabbaabbaabb", 4) // true
checkPattern("abcabcabc", 3) // true
该函数的一些示例用法:

/**
  * Checks for recurring pattern in a String
  * @param s The input String to check
  * @param patternSize The size of the expected pattern.  For example in the String "aabbaabbaabb" the pattern is "aabb" which is a length of 4
  */
def checkPattern(s: String, patternSize: Int): Boolean = {
  val grouped = s.grouped(patternSize)
  grouped.toSet.size == 1 // everything should be the same
}
checkPattern("abababab", 2) // true
checkPattern("aabbaabbaabb", 2) // false
checkPattern("aabbaabbaabb", 4) // true
checkPattern("abcabcabc", 3) // true
因此,对于您的代码,您可以将其与一些保护语句一起使用:

def patternFound(s: String): Boolean = s match {
  case "" => false // empty Strings can't have patterns
  case s if checkPattern(s, 2) => true
  case s if checkPattern(s, 4) => true
  case _ => false
}

patternFound("ababababab") // true
patternFound("aabbaabb") // true
patternFound("aabbzz") // false

编辑:我认为另一个答案更适合您所寻找的内容,但以下是我为您更新的问题提供的更新答案:

def patternFound(s: String): Boolean = s match {
  s.nonEmpty && checkPattern(s, 2)
}

对于您发布的两个示例,这两个正则表达式模式将涵盖它

def patternFound(s:String): Boolean = {
  val ptrn1 = "(.)(.)\\1\\2".r
  val ptrn2 = "(.)\\1(.)\\2".r
  s match {
    case ptrn1(_,_) => true
    case ptrn2(_,_) => true
    case _ => false
  }
}
证明:

patternFound("rrgg")  // res0: Boolean = true
patternFound("hqhq")  // res1: Boolean = true
patternFound("cccx")  // res2: Boolean = false
但我怀疑你的要求,如前所述,不够具体,不能完全涵盖你所寻找的


更新

你现在是第二个要求没有意义了。与第一个模式不匹配的所有内容都将返回
false
,因此测试特定模式是否返回
false
没有意义

def patternFound(s:String): Boolean = {
  val ptrn = "(.)(.)\\1\\2".r.unanchored
  s match {
    case ptrn(_,_) => true
    case _ => false
  }
}
patternFound("babab")   //true because pattern of xyxy found in it
patternFound("baabba")  //false because it doesn't match the target pattern

谢谢你的反馈。我知道这一点。这就是为什么我在寻找我能做什么来匹配特定的模式。在我的例子中,x,y是未定义的字符。嗨@Tyler,我真的很感激这个解决方案,但它并不完全是我想要的,尽管它确实满足了我提出的问题。一旦我有足够的代表,我肯定会投票支持你的解决方案。但我实际上是想创建一个解决方案,该解决方案对于XYXY的模式返回true。。。。或者YXYX。。。但是对于xxyy来说是错误的。。。。或yyxx。。。。你知道我怎样才能有效地做到这一点吗?看来你更新了你的问题。我会更新我的答案。老实说,我很惊讶!这是非常强大和惊人的。但我试图实现的是编写一个函数,它将为字符串“babab”返回true,因为它包含模式xyxy(“babab”的子字符串是匹配重复模式的baba)。如果传递的字符串是“aabba”,则同一函数应返回false,因为其中包含模式xxyy(“aabba”的子字符串是与xxyy=>false匹配的“aabb”)@MoodyCody;请澄清。您的问题说明
匹配“xxyy”=>true
但现在您说“匹配xxyy=>false”。我已相应地更新了问题。措辞可能有点误导。我也在问题中加入了例子来澄清需求。太棒了!工作很有魅力!如果你不介意的话,我可以问一下,为了更好地学习scala,我应该学习哪本书/参考资料(这样我也可以像你那样创建模式匹配)?这个问题更多的是关于正则表达式,而不是scala。这是两件不同的事情。你可能会花一些时间来学习,但我相信网上有很多很好的教程。