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

Gianluigi gradobag at gradobag.it
Tue Aug 22 11:55:57 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]

Hi Lee,

While we wait for Benoit, let me happy and try my simple function and 
tell me if it is so slow that it is snubbed.

I attach the test project (no effort required) :-)



Public Sub Main()

   Dim sText As String = File.Load("./Text1")
   ' Dim sExp As String = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})"
   Dim sExp As String = "((?:[0-9]{1,3}\\.){3}[0-9]{1,3})"
   ' Dim sExp As String = 
   Dim s As String

   For Each s In SimpleFindAll(sExp, sText)
     Print s


Private Sub SimpleFindAll(sPattern As String, sText As String) As String[]

   Dim aReturn As New String[]
   Dim aSplit As String[]
   Dim hRegExp As New RegExp

   Try hRegExp.Exec(sText)
   If Error Then
     Print "#ERROR"
     Return aReturn

   aSplit = Split(sText, "\n", Null, True)
   For i As Integer = 0 To aSplit.Max
     If hRegExp.Text Then aReturn.Push(hRegExp.Text)
   Return aReturn


More information about the User mailing list