Scala 中的多行正则表达式捕获 [英] Multiline regex capture in Scala

查看:36
本文介绍了Scala 中的多行正则表达式捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从多行正则表达式中捕获内容.不匹配.

I'm trying to capture the content from a multiline regex. It doesn't match.

val text = """<p>line1 
    line2</p>"""

val regex = """(?m)<p>(.*?)</p>""".r

var result = regex.findFirstIn(text).getOrElse("")

返回空.

我为多行设置了 m - 标志,但在这种情况下似乎没有帮助.

I put the m - flag for multiline but it doesn't seem to help in this case.

如果我删除换行符,则正则表达式有效.

If I remove the line break the regex works.

我也发现了这个,但无法让它工作.

I also found this but couldn't get it working.

如何匹配

元素之间的内容?我想要两者之间的所有内容,还有换行符.

How do I match the content between the <p> elements? I want everything between, also the line breaks.

提前致谢!

推荐答案

如果你想在 scala 中激活 dotall 模式,你必须使用 (?s) 而不是 (?m)

If you want to activate the dotall mode in scala, you must use (?s) instead of (?m)

(?s) 表示点可以匹配换行符

(?s) means the dot can match newlines

(?m) 表示 ^$ 代表行首和行尾

(?m) means ^ and $ stand for begining and end of lines

这篇关于Scala 中的多行正则表达式捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆