2

I want to extract path from url.

www.abc.com/contact/abc => /contact 
www.abc.com/contact.abc/asdsad => /contact.abc
www.abc.com/contact-abc.html/asdsad => /contact-abc.html
www.abc.com/contact-2 => /contact-2
www.abc.com/contact-us => /contact-us
abc&/contact.abc&^ = /contact.abc

What single regex could do this?

1
  • You could use split at / and get second entry in the array insteed and in abc&/contact.abc&^ you can replace not wished parts. Commented Oct 15, 2016 at 11:35

2 Answers 2

4
\/([0-9a-zA-Z+.-]+)[\/&| ]

This one could be a solution, though it is strictly related to the urls you gave in your question as examples.

I'll leave you the link to regexr so that you can understand what is there actually behind.

Regexr resource

Sign up to request clarification or add additional context in comments.

1 Comment

But It needs to be exactly prefix with /contact. Thank you.
2

If you are using Java, use java.net.URL class to parse URL and getPath method to get its path as explained here, no need for regex:

https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html.

For regex, refer to this in question:

Getting parts of a URL (Regex)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.