Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Match IP address except when preceded by certain characters?

Tags:

python

regex

This regex ([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 doe the job of matching just IP addresses (v4 and v6) and nothing else, but unfortunately, for the text below and similar text, it also picks up the fields in bold:


from mail.example.com (example.com. [213.239.250.131]) by mx.google.com with ESMTPS id xc4si15480310lbb.82.2014.10.26.06.16.58 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 26 Oct 2014 06:16:58 -0700 (PDT)

Received: from ssservices1-1 (192.168.20.142) by mail.supershuttle.com (192.168.20.110) with Microsoft SMTP Server id 14.2.347.0; Tue, 21 Apr 2015

Received: from ssservices1-1 (192.168.20.142) by mail.supershuttle.com (192.168.20.110) with Microsoft SMTP (TLS) Server id 14.2.347.0; Tue, 21 Apr 2015

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


What's the best approaching (I'll be using Python) for omitting these matches? Two are preceded by the text 'id', though in the first case, not directly preceding it.

like image 826
Pyderman Avatar asked Jun 12 '15 04:06

Pyderman


1 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 the ip address is not preceded or followed by ..See demo.

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

like image 148
vks Avatar answered Oct 10 '22 10:10

vks