Getting specific value from url query string using Regex in Java -


I have a string with URL like this:

How do I get the value of the product regular expressions?

I will probably use URLEncodedUtils from App Commands.

  string URL = "http://www.website.com/search?productId=1500"; & Lt; NameValuePair & gt; ParamsList = URLEncodedUtils.parse (new URI, "UTF-8"); (NameValuePair parameter: paramsList) if (parameter.getName (.) Equals ("productId")) System.out.println (parameter getValue ());  

outpit 1500 .

But if you really want to use regex then you can try

  pattern P = Pattern.compile ("[? & Amp;] productId = (\\ d +) "); Matter M = P. Metter (url); // _____________ ^ group 1 if (m.find ()) // | Println (m.group (1));  

Comments