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

T Lee Davidson t.lee.davidson at gmail.com
Mon Aug 21 15:28:11 CEST 2023


On 8/21/23 03:03, Gianluigi wrote:
> Then you need to do this (I attach the example):
> 
> 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 sSplit As String[]
>    Dim sReturn As String
>    Dim hRegExp As New RegExp
> 
>    hRegExp.Compile(sExp)
>    Try hRegExp.Exec(sText)
>    If Error Then
>       Print "#ERROR"
>       Return
>    Endif
> 
>    sSplit = Split(sText, "\n", Null, True)
>    For i As Integer = 0 To sSplit.Max
>      hRegExp.Exec(sSplit[i])
>      sReturn &= hRegExp.Text & " "
>    Next
> 
>    sSplit = Split(sReturn, " ", Null, True, True)
>    For i = 0 To sSplit.Max
>      Print "IP address: " & sSplit[i]
>    Next
> 
> End
> 
> Regards
> 
> Gianluigi

I haven't tried that, but I'm sure it would work.

If I have to parse each text file line by line, then I don't even need to use RegExp. I can simply find the lines like, "IP 
address: x.x.x.x", Split them on the colon and take the second index of the resulting string array (or find the colon with Instr 
and then use Mid to take the address which is to the right of the colon).

That is, in fact, what I have done as I couldn't get RegExp to do what I wanted and didn't want to switch to Python.

I was hoping to be able to do a RegExp global match on the entire text without having to parse it line by line.


-- 
Lee



More information about the User mailing list