My requirement is to get multiple regex patterns in a given String.
"<a href=\"https://page1.google.com/ab-cd/ABCDEF\”>Hello</a> hiiii <a href=\"https://page2.yahoo.com/gr\”>page</a><img src=\"https://image01.google.com/gr/content/attachment/987654321\” alt=\”demo image\”></a><a href=\"https://page3.google.com/hr\">"
With this below code:
val p = Pattern.compile("href=\"(.*?)\"")
val m = p.matcher(str)
while(m.find()){
println(m.group(1))
}
I am getting output:
https://page1.google.com/ab-cd/ABCDEF
https://page2.yahoo.com/gr
https://page3.google.com/hr
With change in Pattern:
val p = Pattern.compile("img src=\"(.*?)\"")
I am getting output:
https://image01.google.com/gr/content/attachment/987654321
But with Pattern:
val p = Pattern.compile("href=\"(.*?)\"|img src=\"(.*?)\"")
I am getting output:
https://page1.google.com/ab-cd/ABCDEF
https://page2.yahoo.com/gr
Null
https://page3.google.com/hr
Please let me know, how to get multiple regex pattern or is their any other easy way to do this?
Thanks