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

Gianluigi gradobag at gradobag.it
Mon Aug 21 00:42:50 CEST 2023


Il 20/08/23 22:29, T Lee Davidson ha scritto:
> Given numerous text files containing one or more ip addresses, I am 
> trying to extract all matches in a given file not knowing how many 
> there may be.
>
> This is the regular expression I am using:
> hRegExp.Compile("(?:IP address: 
> (\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}))")
>
> I have tried compiling with and without RegExp.DotAll, repeating the 
> non-captured group with "+", adding ".*" at beginning and end, and 
> various other quantifiers and pattern tweaks I can't even remember now.
>
> But, no matter what, only the first match is returned.
>
>
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



More information about the User mailing list