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

Gianluigi gradobag at gradobag.it
Tue Aug 22 08:50:54 CEST 2023


Il 22/08/23 00:34, T Lee Davidson ha scritto:
> 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]
>
>
Dear Lee,

I don't want to be right and the last thing I want to do is argue with 
those who always help me.
I love you and admire your many skills as a programmer.

Having said this, I remind you that re.findall is a Python *method*, 
this means that it iterates over the entire string.
Gambas provides you with Compile and Exec to make your re.findall method.

I'm not able to try, but if you feel like doing it, it would be 
interesting to know the result on the same text file.

I have tried with Gambas on very large files and it is very fast.

With sincere regards

Gianluigi



More information about the User mailing list