Anil Bhaskar
Anil Bhaskar

Reputation: 3978

Regex to match optional or fixed subdomain, with optional www, fixed domain

I am trying get a regex to match a url with following rules:

So far I got: ^https:\/\/(w{0,3})(sub1|sub2)\.google\.com$

Can't make it work to allow empty subdomain, i tried (^$|sub1|sub2) in subdomain capturing group, but doesn't work. Also . after www or before domain name, it's conditional.

examples:

Upvotes: 0

Views: 82

Answers (1)

anotherGatsby
anotherGatsby

Reputation: 1598

Try this: ^https:\/\/(w{0,3}\.)?(sub1\.|sub2\.)?google\.com$

Test here: https://regex101.com/r/imwunj/1

In your regex dots after www and sub's were not being matched, so once you make them optional, the regex works.

Upvotes: 1

Related Questions