簡體   English   中英

正則表達式與Scala代碼中的字符串不匹配

[英]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&#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>"

請建議是否還有其他方法可以這樣做。

謝謝

您的表達式似乎很好,在輸入字符串中可能僅包含兩個反斜杠,可能采用以下形式:

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

演示1

或者,如果我們要搜索https模式,可以將其簡化為:

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

演示2

並且我們所需的鏈接在捕獲組#1

正則表達式

如果不需要此表達式,並且希望對其進行修改,請訪問此鏈接regex101.com

RegEx電路

jex.im可視化正則表達式:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM