Home > Blockchain >  Regex to match a series of URLs
Regex to match a series of URLs

Time:02-02

I am trying to write a regex to match a bunch of URL with the structure like:

env1-www-uk.my.domain.com
env2-www-uk.my.domain.com
http://env1-www-uk.my.domain.com
https://env1-www-uk.my.domain.com

There can be different numbers for each environment (env1, env2, env3 etc.) and I would like to match all of them at once. I also need to match everything in the URL which comes afterwards so things like:

env1-www-uk.my.domain.com/something123
env2-www-uk.my.domain.com/some more/stuff-too/

I wrote a regex but it does not work as I expected, could you please point me to the right direction?

^(https:\/\/|http:\/\/)env[0-9]{1}\-www\-uk\.my\.domain\.com. 

CodePudding user response:

I think this will work:

/^(?:htt(?:ps|p):\/\/)?(.*com)(\/?.*)$/gm

check its working here: https://regex101.com/r/WZkFWz/1

Explanation:

^                        start of the line
(?:htt(?:ps|p):\/\/)?    non capture group, which matches either http or https, ? at the end indicates this can be optional
(.*com)                  capturing group, matches everything including after http or from the start of the string till .com 
(\/?.*)                  captures everything after .com till the end of the line
$                        end of the line
  •  Tags:  
  • Related