I have a FTP URL, and have to parse the URL to get the username, password, server name and the directory. What can be the regular exp to do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Whenever I think of regexes, I think “Perl” and write a quick and dirty pattern (qr{xxx}x) and test it against test input.
In your case, assuming that user name, password, server, and directory name all need to be parsed out (and are mandatory), I’d use the following. Add question marks for “optional” parts of your pattern if you wish to modify this:
Now that we have the pattern, simply double the backslash (in the “Question mark” portion), remove spaces and comments (if you wish), and place into a Java String:
Use that with Pattern/Matcher and you should be able to extract things nicely.