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?

progyammer
  • 1,498
  • 3
  • 17
  • 29
Mai Hữu Lợi
  • 559
  • 2
  • 8
  • 18
  • You could use split at / and get second entry in the array insteed and in `abc&/contact.abc&^` you can replace not wished parts. – asdf Oct 15 '16 at 11:35

2 Answers2

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

Cristofor
  • 2,077
  • 2
  • 15
  • 23
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)

Community
  • 1
  • 1
Bambang
  • 391
  • 1
  • 10