helloworld1234
helloworld1234

Reputation: 9

how to include main domain and several subdomains with regex

I am trying to build a regex that looks very similar to this, as for an example,

https://[a-zA-Z].google.comwhich applied to the main domain. I was wondering how do I make it work with including the main domain which is the google.com part?

Upvotes: 0

Views: 43

Answers (1)

Alex Suslyakov
Alex Suslyakov

Reputation: 2222

I'd highly recommend you to read some basics on the regex topic and try out some builders, they are very helpful (my fav https://regex101.com/)

So in your case, first of all let's fix the original regex

  1. Escape special characters https:\/\/[a-zA-Z]\.google\.com
  2. [a-zA-Z] group will match only a single symbol, so you need to add + if you want there to be at least one character in the group or * for any amount of characters (including 0) https:\/\/[a-zA-Z]*\.google\.com
  3. Domain name may contain a dash - and numbers (sometimes even non-ascii characters, but let's not dig into it just yet) https:\/\/[a-zA-Z-0-9]*\.google\.com

Now we're almost there. You can add ? after a symbol in regex to make it optional

https:\/\/[a-zA-Z-0-9]*\.?google\.com will be your final regex

I hope that's clear. Don't hesitate to ask any questions :)

Upvotes: 2

Related Questions