[Gambas-user] RegExp: How to find all matches of a pattern (global match)?

T Lee Davidson t.lee.davidson at gmail.com
Tue Aug 22 00:34:19 CEST 2023


On 8/21/23 11:16, Gianluigi wrote:
> I don't know what's behind the Python code, probably a loop since that's what C seems to do.

No, Python doesn't use a loop; it has a Findall function. It is quite simple and works as one would expect.

[code]
import re

f = open("file_with_ip_addresses.txt")
addresses = re.findall( "((?:[0-9]{1,3}\\.){3}[0-9]{1,3})", f.read() )
for ip in addresses:
     print(ip)
[/code]


-- 
Lee



More information about the User mailing list