此正则表达式不适用于以下给定的字符串,因为以下字符串具有“ 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=25022&page=1\" target=\"_blank\" rel=\"nofollow opener referrer\">https://discussion.xyz.com/thread/250274?answerId=250722&page=1</a></p>"
请建议是否还有其他方法。
谢谢
答案 0 :(得分:-2)
您的表达式似乎很好,输入字符串中可能仅包含两个反斜杠,可能采用以下形式:
href=\\\"(.*?)\\\"
或者,如果我们要搜索https
模式,可以将其简化为:
\\"https?:(.*?)\\"
,我们所需的链接在捕获组#1
中。
如果不需要此表达式,并且希望对其进行修改,请访问regex101.com上的此链接。
jex.im可视化正则表达式: