[英]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=25022&page=1\" target=\"_blank\" rel=\"nofollow opener referrer\">https://discussion.xyz.com/thread/250274?answerId=250722&page=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=\\\"(.*?)\\\"
or if we'd be searching for https
patterns, we could simplify it to: 或者,如果我们要搜索
https
模式,可以将其简化为:
\\"https?:(.*?)\\"
and our desired link is in the capturing group #1
. 并且我们所需的链接在捕获组
#1
。
If this expression wasn't desired and you wish to modify it, please visit this link at regex101.com . 如果不需要此表达式,并且希望对其进行修改,请访问此链接regex101.com 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.