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

Gianluigi gradobag at gradobag.it
Mon Aug 21 17:16:44 CEST 2023


Il 21/08/23 15:28, T Lee Davidson ha scritto:
> 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.
>
>
Hi,

I don't know what's behind the Python code, probably a loop since that's 
what C seems to do.

I hope Benoit answers this.

However that he is already ready or that you do it, believe me that with 
Gambas it is very fast and probably my code can be improved.

I was hoping for once that I was the one useful to the other :-(

Regards

Gianluigi



More information about the User mailing list