Whenever you are using a capturing group, it always returns a submatch, even if it is empty/null. You have 3 capturing groups, so you will always have them in the findall
result.
In regex101.com, you can see these non-participating groups by turning them on in Options:
You may tighten up your regex by removing capturing groups:
(?:[a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|d{1,3}.d{1,3}.d{1,3}.d{1,3}
Or even (?:[a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|d{1,3}(?:.d{1,3}){3}
.
See a regex demo
And since the regex pattern does not contain capturing groups, re.findall
will only return matches, not capturing group contents:
import re
p = re.compile(r'(?:[a-z0-9]{1,4}:+){3,5}[a-z0-9]{1,4}|d{1,3}.d{1,3}.d{1,3}.d{1,3}')
test_str = "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
<[email protected]> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256
bits=128/128); Sun, 26 Oct 2014 06:16:58 -0700 (PDT)"
print(re.findall(p, test_str))
Output of the online Python demo:
['213.239.250.131', '014.10.26.06']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…