[英]Regex not matching for a String in Scala code
此正則表達式不適用於以下給定的字符串,因為以下字符串具有“ 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>"
請建議是否還有其他方法可以這樣做。
謝謝
您的表達式似乎很好,在輸入字符串中可能僅包含兩個反斜杠,可能采用以下形式:
href=\\\"(.*?)\\\"
或者,如果我們要搜索https
模式,可以將其簡化為:
\\"https?:(.*?)\\"
並且我們所需的鏈接在捕獲組#1
。
如果不需要此表達式,並且希望對其進行修改,請訪問此鏈接regex101.com 。
jex.im可視化正則表達式:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.