简体   繁体   中英

Regex not matching for a String in Scala code

This Regex is not working for below given string, as below string has “href”. I am using 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

or if we'd be searching for https patterns, we could simplify it to:

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

Demo 2

and our desired link is in the capturing group #1 .

RegEx

If this expression wasn't desired and you wish to modify it, please visit this link at regex101.com .

RegEx Circuit

jex.im visualizes regular expressions:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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