1

I know there are tons of domain matching regular expressions floating around, but couldn’t find one to answer my particular question. I’m looking for a regular expression that will match only a URL’s domain name, but nothing else (not even the TLD). It doesn’t need to validate the domain.

So given the sample below:

https://www.orchardsoft.com
https://www.horizon-lims.com/contact/us
https://www.quartzy.com
https://qbench.net
https://www.xifin.com

...the regular expression needs to match for the following:

Orchardshot
Horizon-lims
Quartz
QBench
Xifin

The regular expression I'm starting with is this: (.|//(\w+.)+

Is anyone able to point me in the right direction?

  • See [Regex match a hostname — not including the TLD](https://stackoverflow.com/questions/836536/regex-match-a-hostname-not-including-the-tld). – Wiktor Stribiżew Feb 06 '19 at 20:21
  • @WiktorStribiżew I tried the regular expressions suggested in that post, but unfortunately they don't take into account things like domain protocol. Thank you though! – Micah Owens Feb 06 '19 at 20:39
  • Are you doing this server side/with a specific language? If so many server side languages like Java servlets have all sorts of goodies to make life easy doing this kind of stuff. – JGFMK Feb 06 '19 at 20:42
  • @JGFMK I'm not unfortunately. I'm just running a match text action within an application called Shortcuts which can use Regular Expressions for matching & replacing text. – Micah Owens Feb 06 '19 at 20:47
  • You should better use example domains for illustration as described in [RFC 2606](https://tools.ietf.org/html/rfc2606#page-2), e.g. `https://www.something-hyphened.example/contact/us`. – Matthias Feb 06 '19 at 22:29

1 Answers1

0

As long as you declare the possible TLDs (here: .com.tr, .com, .net), you can use this regex:

 ([\w-]+)(?=\.(?:com\.tr|com|net))

In fact, an FQDN has a hierarchical structure which makes it impossible to always analyze it correctly with a regex. It would fail (match twice) for entries that contain a TLD in its path like https://www.example.com/a.combination.

Matthias
  • 7,432
  • 6
  • 55
  • 88