regex: URL thingy with username, password, host and port

July 7, 2008 by Michael

Just in case i do keep forgetting that stuff, here’s a regex for decoding urls like


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);
	throw new RuntimeException("Invalid ftp url!");			
final String protocol =;
final String user     =;
final String password =;
final String host     =;
final int port = != null ? Integer.parseInt( : 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.

One comment

  1. maurice wrote:

    Your post was useful for me, and I want to thank you for that. Because in my case I was trying to apply it for http/https protocols, I hit the situation were there can be a port number. Like ‘8080’ in So I had to adapt your regex pattern. But please note that according to, you could have a port specified in an ftp uri, too. Greetings from Spain!

    Posted on September 23, 2016 at 9:47 PM | Permalink
Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *