Fetching package name from string input - java

Hi:

I need to write a function which accepts a url as string. The function should do the following:

If the input is of the first url pattern, it should get the content after the last '/' in the url and before the .apk

If the input is of the second url pattern, it should get the content after the ?id= and before &feature


ie; in short, it should be returning package name from url. How can I accomplish this through java? Any help is appreciated.

PATTERN 1

http://abc.com/data/applications/net.feathertech.mediterraneanrecipes.apk
http://abc.com/data/applications/com.a731412505508e759b5b9717a.a36562546a.apk
http://abc.com/data/applications/com.petwisewebsites.mobile.apk

PATTERN 2

https://play.google.com/store/apps/details?id=com.apofiss.yinthecatlite&feature=personalized-recommendations#?t=W251bGwsMSwxLDIxMywiY29tLmFwb2Zpc3MueWludGhlY2F0bGl0ZSJd


https://play.google.com/store/apps/details?id=com.apofiss.pandadumplinglite&feature=more_from_developer#?t=W251bGwsMSwxLDEwMiwiY29tLmFwb2Zpc3MucGFuZGFkdW1wbGluZ2xpdGUiXQ..

https://play.google.com/store/apps/details?id=com.apofiss.chameleon&feature=more_from_developer#?t=W251bGwsMSwxLDEwMiwiY29tLmFwb2Zpc3MuY2hhbWVsZW9uIl0.
Sreejith22Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
 s = s.replaceAll(".*/([^&]+).*", "$1").replaceAll(".*=(.*)", "$1");

Open in new window

The above is possibly OK, although it should be possible to do it more elegantly
0
 
Sreejith22Author Commented:
please let me know if any clarification regarding question is required
0
 
Sharon SethCommented:
use regex
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Sreejith22Author Commented:
any code snippet is much appreciated. I know regex is the possible way, but , sorry I do not have expertise using it.
0
 
Sreejith22Author Commented:
Hi cehj,
 it does not exclude apk in first pattern
0
 
CEHJCommented:
Well you didn't ask for that ;)

Quick and dirty - tack this on at the end

.replaceAll("\\.apk$", "");

Open in new window

0
 
CEHJCommented:
:)
0
All Courses

From novice to tech pro — start learning today.