-1

For my situation, I need to extract the IP address or the hostname from the list. For example, suppose this is the list

d-bns-teaut-12.corporate.net:9100, 10.212.142.61:9100, d-bns-teaut-23.corporate.net:9100, 10.212.142.87:9100, d-bns-teaut-25.corporate.net:9100, 10.212.142.98:9100

I need to extract

d-bns-teaut-12,10.212.142.61,d-bns-teaut-23,10.212.142.87,d-bns-teaut-25 and 10.212.142.98

So far I know how to extract either the IP or hostname, but not both with a regex at the same time.

I tried with the expression /([a-zA-Z0-9\.-]+)/ and with that I am able to extract the IP and if I add anymore regex patter to extract the .corporate.net, I do not get the IP in the results.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Ashish Kurian
  • 51
  • 5
  • 12
  • do you have IPv6 addresses? are all the addresses exclusively a valid hostname or IP or are there other values? in such a case, try an IP parser (rejected addresses are a hostname) or if there are no IPv6 addresses and exclusivity, just checking for any letter (must exist in TLD, while a normal IPv4 address cannot contain letters) is sufficient – ti7 Jan 17 '22 at 21:24
  • Does `([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[^.]+)` work? Also, try `^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[^.]+)` and `([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[^.]+).*` – Wiktor Stribiżew Jan 17 '22 at 21:45
  • Does this answer your question? [Grafana Legend format :9100 removal](https://stackoverflow.com/questions/47293510/grafana-legend-format-9100-removal) – ti7 Jan 17 '22 at 22:41
  • @WiktorStribiżew : Thanks Wiktor. `([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[^.]+)` worked perfectly for my use case. Now it extracts all the IP and also the hostname from the list. Thanks again – Ashish Kurian Jan 18 '22 at 07:56
  • @ti7 : I do not have IPv6 address and also your suggested question is not what I wanted. That answers my question only partially. I really appreciate your help though. – Ashish Kurian Jan 18 '22 at 07:57

1 Answers1

0

You can use

([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|[^.]+)

The pattern is a capturing group ((...)) that matches either

  • [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} - one, two or three digits and three other occurrences of . and one, two or three digits
  • | - or
  • [^.]+ - one or more chars other than . char.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563