Regex: match IP address unless specific characters precede?

This is a regular expression ([a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3 } almost completed the task of matching only IP addresses (v4 and v6) and nothing else, but, unfortunately, for the text below and similar text, it also displays fields in bold


from mail.example.com (example.com. [213.239.250.131]) via mx.google.com with ESMTPS ID xc4si15480310lbb.82.2 014.10.26.06 .16.58 for (version = TLSv1.2 cipher = ECDHE-RSA-AES128-GCM -SHA256 bit = 128/128); Sun, Oct 26, 2014 6:16:58 a.m. -0700 (PDT)

Received: from ssservices1-1 (192.168.20.142) from mail.supershuttle.com (192.168.20.110) with Microsoft SMTP server ID 14.2.347.0 ; Tue, 21 April 2015

Received: from ssservices1-1 (192.168.20.142) from mail.supershuttle.com (192.168.20.110) with Microsoft SMTP server identifier (TLS) 14.2.347.0 ; Tue, April 21, 2015

Received: from plug.mysitehosted.com (plug.mysitehosted.com [10.248.1.153]) (using TLSv1 with DHE-RSA-AES256-SHA encryption) to 0.0.0.0 : 2500 (trex / 5.0.19); Tue, March 11, 2014 06:14:03 GMT


What is the best approximation (I will use Python) to exclude these matches? The two are preceded by the text "id", although in the first case it is not directly in front of it.

+6
source share
2 answers
 ([a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|(?<!id )(?<!\.)\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b(?!\.) 

You can try this. Through lookaheads we make sure that the IP address does not precede or follow . . See the demo.

https://regex101.com/r/hI0qP0/3

+4
source

As an additional hint, you can add a positive search back and forth. They will allow you to see what is before and after the match without consuming anything. Your IP addresses are always surrounded by () or [] , so you can try:

 (?<=\[|\()your regex(?=\]|\)) 

This will match your regex in both:

 (your regex) [your regex] 
+2
source

Source: https://habr.com/ru/post/988967/


All Articles