[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 05:10:38 CEST 2023


On 8/20/23 18:42, Gianluigi wrote:
> Something like that?
> 
> Public Sub Main()
> 
>    Dim sText As String = "abcdefty 192.15.122.38. dertgbjo 192.1.34.1. greantyop 189.1.76.238. zxcvbqerty 12.15.11.1. sawqrtui 
> 255.7.6.138."
>    Dim sExp As String = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})"
>    Dim sSplit As String[]
>    Dim hRegExp As New RegExp
> 
>    hRegExp.Compile(sExp)
>    Try hRegExp.Exec(sText)
>    If Error Then
>       Print ("#ERROR")
>       Return
>    Endif
> 
>    sSplit = Split(sText, " ", Null, True)
>    Stop
>    For i As Integer = 0 To sSplit.Max
>      hRegExp.Exec(sSplit[i])
>      Print hRegExp.Text;;
>    Next
> 
> End
> 
> Regards
> 
> Gianluigi


Well, not exactly, but I thank you for trying.

The text to parse actually looks more like this:
A bunch of multi-line text to ignore.
IP address: 192.15.122.38
More multi-line text to ignore.
IP address: 192.1.34.1
More multi-line blah blah blah.
IP address: 189.1.76.238

There are other places in the text where an IP address pattern could occur, but I need only the address on lines that start with 
"IP address:". That's why I was using the pattern: "(?:IP address: (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))"

I have accomplished what I needed by parsing the text line by line and testing if the line Begins with "IP address:". But, it 
just seems there should be some way to tell Gambas to do a global match, as in other languages, instead of simply returning the 
first match. Or, perhaps I don't know regular expressions as well as I'd like, and I am missing something in my pattern.

I didn't mention it previously, but I did try with the RegExp.MultiLine compile option which also seemed to make no difference.


-- 
Lee



More information about the User mailing list