简体   繁体   English

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

[英]Regex not matching for a String in Scala code

This Regex is not working for below given string, as below string has “href”. 此正则表达式不适用于以下给定的字符串,因为以下字符串具有“ href”。 I am using scala 2.11.11 我正在使用scala 2.11.11

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

String: 串:

"<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>"

Please suggest if any other way to do this. 请建议是否还有其他方法可以这样做。

Thanks 谢谢

Your expression seems to be fine, there are just two backslashes in the input string that can be likely included, maybe in this form: 您的表达式似乎很好,在输入字符串中可能仅包含两个反斜杠,可能采用以下形式:

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

Demo 1 演示1

or if we'd be searching for https patterns, we could simplify it to: 或者,如果我们要搜索https模式,可以将其简化为:

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

Demo 2 演示2

and our desired link is in the capturing group #1 . 并且我们所需的链接在捕获组#1

RegEx 正则表达式

If this expression wasn't desired and you wish to modify it, please visit this link at regex101.com . 如果不需要此表达式,并且希望对其进行修改,请访问此链接regex101.com

RegEx Circuit RegEx电路

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM