0

I would like to negate this regex that matches if the ip is 1.2.3.4 and followed by 'myhost' but i want it to match if the ip is not 1.2.3.4 but it is followed by myhost

^(1\.2\.3\.4) (?=myhost)

I tried doing this but it does not work.

^(?!(1\.2\.3\.4)) (?=hostname)

I also would like to negate this regex that matches the hostname but I would like to match if the hostname is not "myhost"

(?<=1\.2\.3\.4) (myhost)

the regex matches myhost if its preceeded by 1.2.3.4

the idea is to check some /etc/hosts files and make sure the ip matches the hostname and viceverza.. I will be doing this in ansible.. so if the regex can be combined in one line..
in a nutshell i want to test that are always correct. I am using ansible fact to find the ip and hostname.. i have just problem with the regexes.

edit: as requested some examples:

i have the following 4 lines:

1.2.3.4 hostname (1)
1.2.3.5 hostname (2)
1.2.3.4 otherhost (3)
1.2.3.5 otherhost (4)

line (1) nothing should match. in lines (2) it should match the ip address but without the espace after it. in line (3) otherhost should match. In line (4) both should match..

If any of the regex match it will be replaced with ansible with line (1) this is to make sure the /etc/host is correct. does not have typos or inconsistencies.

thank you.

  • Is this working for you? – Toto Feb 6 '19 at 14:05
0

Lookarounds are zero-length assertion, you have to add regex that matches IP:

^(?!1\.2\.3\.4)\d+(?:\.\d+){3} (?=hostname)

This will match IP address followed by hostname that is not equal to 1.2.3.4

  • and how to do the opoosite match the hostname that is preceded by ip 1.2.3.4 that is not hostname. and is it possible to combine the two in one? – danidar Feb 6 '19 at 12:15
  • @danidar: Sorry, I don't get you. Please, editi your question and add some examples that you want to match and some that must not. – Toto Feb 6 '19 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.