How do I extract /path/index.html from following string
url("/path/index.html")
url(/path/index.html)
I using following regex, but don't work.
Pattern.compile("\\b(href=|url\\()([\"\'])(([^/]+://)([^/<>]+))?([^\"\'>]*)([\"\']|\\))", Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ);
Like some people above stated already, it is better to use the built-in URI helper class. However, if you really need to use a regex for that(and the strings you provided are all the available cases), something like this should do it for you:
/url\("?(.*?)"?\)/
It should look something like this when you pass the pattern as a string in java:
"url\\(\"?(.*?)\"?\\)"
You can then find the string you're looking for in the matched groups.
This will satisfy your requirement.
String url = "https://stackoverflow.com/questions/45545336/how-extract-path-from-string-using-regex";
String[] pieces = url.replaceFirst("://", "/").split("/");
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.