Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need regex help.

I have this url (for example)

https://www.mysite.site/uk/en/watches/automatic/

I want to identify if the second folder contains 'en' (true or false)

How do I do that with regex?

What I have tried:

Numerous

https?:\/\/[^/]+?\/[^/]{2}\/

https?:\/\/[^/]+?\/eu\/

path rx:\/[\w]{2}$
Posted
Updated 24-May-21 6:20am

Try:
(?<=https?:\/\/.*?\/.*?\/)en(?=\/)

If the regex matches, the second folder is "en". If it doesn't, it isn't.

If you are going to work with regular expressions, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Assuming your using extended POSIX, you were very nearly there with the first example posted: The following seems to work for me:
https?:\/\/[^/]+?\/[^/]+/en/
If you want to capture the 2nd path element, then the following sed extended POSIX regex also works
sed -E -e "s/https?:\/\/[^/]+?\/[^/]+\/([^/]+)\/.*/\1/"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900