I have a number of strings that could look anything like this
apl22002.prod.f02.dc2.example.com - DC2H2
N2 - apl220010.prod.f02.dc2.example.com
I would like a regex to match just the hostname, e.g. apl22002 in the first line and apl220010 in the second line.
I tried ^([^.]+)
which works for the first line, but I am not sure how to get it to work with the second line.
I tried ([\w_-]+(?:(?:\.[\w_-]+)+))
which matches the FQDNs nicely, but does not match the hostnames exclusively.
Can someone please point me in the right direction?
Thanks!
This regex will match any string that has at least 1 DOT character in the middle, such as "outlook.com". It will only capture the first portion (hostname). You almost had it with the 2nd regex, but grouped the entire string. I note you included the underscore character "_", if that is necessary, add it to my regex below.
([\w-]+)(?:\.[\w-]+)+