How to pull string value in url using scala regex? -
i have below urls in applications, want take 1 of value in urls.
example:
rapidvie value 416
input url: http://localhost:8080/bladdey/shop/?rapidview=416&projectkey=dsci&view=detail&
output should be: 416
i've written code in scala using import java.util.regex.{matcher, pattern}
val p: pattern = pattern.compile("[?&]rapidview=(\\d+)[?&]")**strong text** val m:matcher = p.matcher(url) if(m.find()) println(m.group(1))
i getting output, want migrate scala using scala.util.matching library.
how implement in simply?
this code working java utils.
in scala, may use unanchored
regex within match
block captured part:
val s = "http://localhost:8080/bladdey/shop/?rapidview=416&projectkey=dsci&view=detail&" val pattern ="""[?&]rapidview=(\d+)""".r.unanchored val res = s match { case pattern(rapidview) => rapidview case _ => "" } println(res) // => 416
see scala demo
details:
"""[?&]rapidview=(\d+)""".r.unanchored
- triple quoted string literal allows using single backslashes regex escapes, ,.unanchored
property makes regex match partially, not entire stringpattern(rapidview)
gets 1 or more digits part (captured(\d+)
) if pattern finds partial matchcase _ => ""
return empty string upon no match.
Comments
Post a Comment