regex: URL thingy with username, password, host and port
Just in case i do keep forgetting that stuff, here’s a regex for decoding urls like
ftp://user:somepass@somehost:someport
in Java:
final Hashtable<String, Integer> portMap = new Hashtable<String, Integer>();
portMap.put("ftp", 21);
portMap.put("sftp", 22);
final Pattern urlPattern = Pattern.compile("(ftp|sftp)://(\\S+):(\\S+)@([\\S&&[^:]]+)(:(\\d+))?");
final Matcher m = urlPattern.matcher(url);
if(!m.matches())
throw new RuntimeException("Invalid ftp url!");
final String protocol = m.group(1).toLowerCase();
final String user = m.group(2);
final String password = m.group(3);
final String host = m.group(4);
final int port = m.group(6) != null ? Integer.parseInt(m.group(6)) : portMap.get(protocol); |
Just in case anybody is interessted, i’m writing a wrapper around j2ssh and Commons::Net to support both ftp and sftp in a J2SE program.
Share This
Just in case i do keep forgetting that stuff, here’s a regex for decoding urls like ftp://user:somepass@somehost:someport in Java: final Hashtable<String, Integer> portMap = new Hashtable<String, Integer>(); portMap.put("ftp", 21); portMap.put("sftp",...
— Trackback URI
This entry (permalink) was posted on Monday, July 7, 2008, at 9:56 am by Michael, tagged with Code Snippets and categorized in English posts, Java.
The following post could be of some interest: Using SQL*Plus without tnsnames.ora, Grails startup parameter, Oracle 11g: Default case-sensitive passwords, How to wake a sleepy mac…, Note to myself: Get available physical memory of a xen host, Remote hdd cloning, Today: Fun with Unicode, Regex and Java., A weird hotline call…, Virtualization with load-balancing and hot-failover: Done., MySql compatible AES encryption / decryption in Java
Post a Comment