【发布时间】:2015-10-31 03:56:19
【问题描述】:
我正在尝试使用正则表达式和模式匹配从字符串中提取值:
val reg = """((?<=a)b)""".r
"ab" match { case reg(x) => x }
无论我如何尝试,它仍然会引发 MatchError。但是,如果我尝试以下方法:
reg.findAllIn("ab").mkString
正则表达式的行为符合预期:res28: String = b
当然,我可以简单地更改正则表达式并添加另一个组:
val reg = """(a)(b)""".r
"ab" match { case reg(_,x) => x }
但我想知道是否可以在模式匹配中使用前瞻/后向运算符。
提前谢谢你。
【问题讨论】:
标签: regex scala pattern-matching