正则表达式与Scala代码中的字符串不匹配

时间:2019-06-07 13:48:27

标签: regex scala

此正则表达式不适用于以下给定的字符串,因为以下字符串具有“ href”。我正在使用scala 2.11.11

val p1 = Pattern.compile("href=\"(.*?)\"")
    val m1 = p1.matcher(bodyString)
    while(m1.find()){
        println(m1.group(1))
    } 

字符串:

"<p>Is this person trying to advertise a sound card? They dont seem to be answering my questions either </p><p><br /></p><p><a href=\"https://discussion.xyz.com/thread/2524?answerId&#61;25022&amp;page&#61;1\" target=\"_blank\" rel=\"nofollow opener referrer\">https://discussion.xyz.com/thread/250274?answerId&#61;250722&amp;page&#61;1</a></p>"

请建议是否还有其他方法。

谢谢

1 个答案:

答案 0 :(得分:-2)

您的表达式似乎很好,输入字符串中可能仅包含两个反斜杠,可能采用以下形式:

href=\\\"(.*?)\\\"

Demo 1

或者,如果我们要搜索https模式,可以将其简化为:

\\"https?:(.*?)\\"

Demo 2

,我们所需的链接在捕获组#1中。

RegEx

如果不需要此表达式,并且希望对其进行修改,请访问regex101.com上的此链接。

RegEx电路

jex.im可视化正则表达式:

enter image description here