2

Hi folks I've been at this for 2 hours and figured there has to be someone out there with a better solution?

In this regex case, I need to match all domains and subdomains from bbc. Eg:

bbc.com
subdomain.bbc.co.uk

But not sites such as

another-bbc.com
anotherbbc.com
bbcmore.com

I need to keep the "bbc" part flexible so I can use other domains in future such as facebook.

The closest Ive got to is (.*\.)?bbc\..* but it's matching anotherbbc.

Please can you help me? Would appreciate this.

CodeUK
  • 421
  • 1
  • 5
  • 13
  • 1
    You may try `(.*\.)?(?<=\.|^)bbc\..*` ([demo](https://regex101.com/r/bbosyW/1)) or (with separate strings, not at the online tester) `(.*\.)?(?<![^.])bbc\..*` – Wiktor Stribiżew Mar 12 '20 at 10:41
  • Which flavour? `PCRE` (e.g. `PHP`) ? – Jan Mar 12 '20 at 10:41
  • 2
    I think that adding `^` should work: `^(.*\.)?bbc\..*`. – Maroun Mar 12 '20 at 10:42
  • Thanks all, its likely PHP. I am using Pi-Hole DNS. Thanks again – CodeUK Mar 12 '20 at 10:43
  • @Wiktor, that works an absolute charm my friend – CodeUK Mar 12 '20 at 10:44
  • Doesn't need to be as complex as the first comment makes it; if you change the `.*` into `.+` it will match one or more characters rather than zero or more, which should solve your problem. – Spudley Mar 12 '20 at 10:44
  • 1
    Yes, but my pattern is equal to `^(.*\.)?bbc\..*`, @Maroun's comment is an improvement. They are equal in the terms of result. – Wiktor Stribiżew Mar 12 '20 at 10:44
  • 1
    @Maroun, this solution also works a charm. Thank you so much everyone. While you're here, check out Pi-Hole its a great tool to help cut down tracking and analytics. I know as devs we have to work with them but for home use, theyre worth every bit. – CodeUK Mar 12 '20 at 10:46

1 Answers1

0

The following one should suit your needs:

^([^.\r\n]+\.)*(bbc\.com|bbc\.co\.uk)$

Allows any amount of subdomains for the hardcoded domains.

For this specific case, you could replace (bbc\.com|bbc\.co\.uk) with bbc\.(com|co\.uk), but if you need other domains as well (e.g. like you said, facebook.com), I believe it will be more maintainable to hardcode them entirely.

See https://regex101.com/r/PnMYpY/1.

sp00m
  • 47,968
  • 31
  • 142
  • 252