【发布时间】:2018-03-15 23:49:00
【问题描述】:
我是 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
谁能举例说明我如何做到这一点?
正在寻找一种解决方案,该解决方案对于字符串中出现的任何 xyxyxy 模式返回 true,但在该字符串中的模式为 xxyy 时返回 false。
示例:如果字符串是“babab”或 “ababa”(其中包含模式 xyxy),但为“aabba”返回 false 或“bbaab”(其中包含模式 xxyy)
感谢任何帮助!提前谢谢你。
【问题讨论】:
标签: regex scala functional-programming pattern-matching