Hi I am trying to get a String out of URL.
I am using Regular Expression.
String url = "http://localhost/htc/android/htc-incredible/259164-gpid";
Pattern regex = Pattern.compile("^.+/(\\d+)-gpid$");
Matcher tagmatch = regex.matcher( url );
System.out.println(tagmatch.group(0));
Error:
Exception in thread "main" java.lang.IllegalStateException: No match found
What I am doing wrong:
You need to use group(1)
(the contents of the first capturing group), not group(0)
(the entire match).
Oh, and of course you need to actually do a search:
tagmatch.find();
System.out.println(tagmatch.group(1));
You could try this:
String url = "http://localhost/htc/android/htc-incredible/259164-gpid";
url.substring(url.indexOf('/',url.indexOf("htc-incredible/"))+1,url.indexOf("-gpid"));
Alternatively, you can use lastIndexOf
in combination with split
to get the part of the url you want. Code will look like this
url.substring(url.lastIndexOf('/')+1).split("-")[0] //prints 259164
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.