I need to be able to convert:
(url) http://www.joe90.com/showroom
to
(namespace) com.joe90.showroom
I can do this using tokens etc, and a enforced rule set.
However, is there a way (a java package) that will do this for me? or do i need to write one myself?
Thanks
From stackoverflow
-
java.net.URL url = new java.net.URL("http://www.joe90.com/showroom"); String tokens[] = url.getHostname().split("."); StringBuilder sb = new StringBuilder(); for (int i=0; i<tokens.length; i++) { if (i > 1) { sb.append('.'); } sb.append(tokens[i]); } String namespace = sb.toString();Alternatively you can parse the hostname out.
Pattern p = Pattern.compile("^(\\w+://)?(.*?)/"); Matcher m = p.matcher(url); // string if (m.matches()) { String tokens[] = m.group(2).split("."); // etc }Of course that regex doesn't match all URLs, for example:
http://username@hostname.com/...That's why I suggested using java.net.URL: it does all the URL validation and parsing for you.
Tom Hawtin - tackline : What's with the -1?joe90 : to not put the . before the first entry.. -
Your best bet would be to split the string based on the
.and/characters (e.g. usingSting.split(), and then concatenate the pieces in reverse order, skipping over any you don't want to include (e.g.www)
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.