regular expression - parent domain only

sirbounty
sirbounty used Ask the Experts™
on
Looking for a regex that will match the parent domain only.
in examples like the following
abcdefg.com   =  abcdefg.com
abcdefg.cn  =      abcdefg.cn
abc.def.org   =  def.org
abc-def.ghi.uk  = ghi.uk

This is the output of a cmdlet that won't produce any http:// prefix, nor any trailing pages - just the domain info, but I only want to extract the parent. Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

pls try

\w+\.\w+$

Open in new window

EDIT and for .co.uk or .co.jp

\w+\.(\w+|co\.\w{2})$

Open in new window

Regards
Top Expert 2007

Author

Commented:
I don't know enough regex to know why it's not working, but it doesn't seem to be picking out that information.
I've tried both in powershell and regextester.com
Top Expert 2016

Commented:
Don't understand it works in regextester.com

EE
how do you use it?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Top Expert 2007

Author

Commented:
I have these examples in regextester:

abc-def.ghi.uk
facebook.com
somewhere.somewhereelse.com
abcdefg.com
abcdefg.cn
abc.def.org


Should it highlight just one, or any matches?
I'll be pulling these from an array, if that matters?
Top Expert 2007

Author

Commented:
If I take off the trailing $, it matches the def.ghi portion of this, but I would need ghi.uk from it...doesn't match anything with the $ left on.

abc-def.ghi.uk
Top Expert 2007

Author

Commented:
Weird - same regex and test as your screen shot, but it fails on mine with the exact same data?

regextest.png
Top Expert 2016

Commented:
Is there a space after org?
When you're trying to match TLD's and also catching country TLD's its a bit of a pain. I would use an exclusion match on the delimiter..

I've included all of the valid country TLD's (as per the Wikipedia article on the subject) and one with just the .uk TLD. I wouldn't use \w for the TLD as I don't think you'll find a TLD with a number in it.

UK TLD only
[\w-]+\.[a-zA-Z]+(\.uk)?$

Open in new window

All country TLD's
[\w-]+\.[a-zA-Z]+(\.(ac|ad|ae|af|ag|ai|al|am|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw))?$

Open in new window


If you are really keen you can probably go ahead and make sure that no rules are being broken like domain names starting, ending or containing multiple hyphens in sequence. Examples.

-bad-domain.com
bad--domain.com
baddomain-.com
-reallybaddomain-.com
good-domain.com
Top Expert 2007

Author

Commented:
Thanks to you both

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial