2 Answers2

0

You could try using String#replaceAll for a one-line solution:

String url = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";
String businessId = url.replaceAll(".*[&?]businessId=([^=?&]+)\\b.*", "$1");
System.out.println(businessId);

This prints:

5d8648b561abf51ff7a6c189

Actually Apache has a number of libraries that can make handling your requirement much easier. On Android, the following might work:

Uri uri = Uri.parse(url);
String linkParam = uri.getQueryParameter("link");
Uri uri2 = Uri.parse(linkParam);
String businessId = uri2.getQueryParameter("businessId");
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

I noticed android tag in your question. You can parse the Url string to Uri and then you can get query param.

String myUrl = "https://example.com/?link=https://play.google.com/store/apps/details?id=com.example.cp&hl=es&apn=com.picker.cp&st=Share+this+app&utm_source=AndroidApp?businessId=5d8648b561abf51ff7a6c189";

Uri uri = Uri.parse(myUrl);
String businessId = uri.getQueryParameter("businessId");
www.hybriscx.com
  • 1,129
  • 4
  • 22
  • Are you sure about this? The `businessId` query parameter is part of another URL which itself is part of the actual URL. – Tim Biegeleisen Oct 27 '19 at 02:46
  • Haven't tried it but I assume it should work. Kindly share any gap you notice. And If it doesn't then it may be easy to do a regex match to find expected value. What do you think? – www.hybriscx.com Oct 27 '19 at 02:51