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 string
  • pattern(rapidview) gets 1 or more digits part (captured (\d+)) if pattern finds partial match
  • case _ => "" return empty string upon no match.

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -