我想在Scala中使用Regex非捕获组,简称为“?:”。
经过数小时的各种案例测试,我来到这里寻求解决方案。我找到了这个问题及其答案,但对我没有用。模式匹配时是否可以使非捕获组在scala正则表达式中工作
因此,我写了一个最小的示例来测试上述线程的语句。
val test = ("""(?:<.*>)(.*)(?:<.*>)""".r findFirstIn ("<test>hello</test>")) getOrElse ""
println("DEBUG MESSAGE (test): " + test)
预期产量: hello
实际输出: <test>hello</test>
那么我的代码有什么问题呢?
尝试使用此正则表达式:
<.*?>(.*?)<.*?>
Scala代码
另外,请尝试使用以下代码来获取捕获组#1的内容,如下所示:
val string = "<test>hello</test>"
val pattern = """<.*?>(.*?)<.*?>""".r
pattern.findAllIn(string).matchData foreach {
m => println(m.group(1))
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句