About regex

  1. 7 days ago

    Aditya N

    Nov 7 Pre-Release Testers, Xojo Pro

    I have the following data

    WARNING: The runtime version supported by this application is unavailable.
    Using default runtime: v4.0.30319
    AssemblyRef Table
    1: Version=2.0.0.0
            Name=mscorlib
            Flags=0x00000000
            Public Key:
    0x00000000: B7 7A 5C 56 19 34 E0 89
    2: Version=2.0.0.0
            Name=System.Windows.Forms
            Flags=0x00000000
            Public Key:
    0x00000000: B7 7A 5C 56 19 34 E0 89
    3: Version=2.0.0.0
            Name=System
            Flags=0x00000000
            Public Key:
    0x00000000: B7 7A 5C 56 19 34 E0 89
    4: Version=2.0.0.0
            Name=System.Drawing
            Flags=0x00000000
            Public Key:
    0x00000000: B0 3F 5F 7F 11 D5 0A 3A

    I only need all of the data on the line and after "Name=" so in this case are "mscorlib","System.Windows.Forms","System","System.Drawing", for each matching regex result i need to assign it into the string array, i need the regex match patter to be infinitely match multiple lines.

    Currently i have the following end :

    'Prepare a regular expression object
    Dim myRegEx As RegEx
    Dim myMatch As RegExMatch
    Dim result() As String
    Dim counter As Integer
    Dim temp As Integer = 0 
    Dim tempCount As Integer = 0 
    myRegEx = New RegEx
    myRegEx.Options.TreatTargetAsOneLine = False
    myRegEx.SearchPattern = "[\n\r].*Name=\s*([^\n\r]*)"
    'Pop up all matches one by one
    myMatch = myRegEx.Search(txtAreaInput.Text)
    //tempCount = myMatch.SubExpressionCount + 1
    //Redim result(tempCount)
    //MsgBox(result.Ubound.ToText)
    For counter = 1 To myMatch.SubExpressionCount - 1
      MsgBox(myMatch.SubExpressionString(0))
      //myMatch = myRegEx.Search()
      //MsgBox(counter.ToText)
      //result(counter) = myMatch.SubExpressionString(counter+1)
      result.Insert(counter, myMatch.SubExpressionString(counter))
    Next counter 
    //MsgBox(myMatch.SubExpressionCount.ToText)
    For temp = 0 To result.Ubound
      txtAreaResult.AppendText(result(temp))
    Next temp
    

    As above code, i always stuck on assigning the each matched pattern into the result array, also the regex is stopped at "Name=System", it doesn't regex until the end of txtAreaInput.Text.

    Any help is really appreciated.

    It's an easy mistake to make (I did it myself initially) but you've confused SubExpressionCount with matches. If you have a pattern like "(a)(b)(c)" and use that on the text "abc", you will get a single match with SubExpressionCount = 4, SubExpressionString( 0 ) = "abc", SubExpressionString( 1 ) = "a", etc. If you need to get the next match of "abc", you have to call Search again.

    dim rx as new RegEx
    rx.SearchPattern = "some pattern"
    dim match as RegExMatch = rx.Search( myText )
    while match isa object
      // do something with match
      match = rx.Search // Get the next
    wend

    In your case, I'd using this pattern:

    ^ +Name=(.*)

    For each match, assign SubExpressionString( 1 ) to your array.

    To break that down, "^" is an anchor that matches the start of the line, " +" is one or more spaces, "Name=" is literal text, and "(.*)" will capture all the remaining text to the end of the line into group 1.

  2. Jean-Yves P

    Nov 7 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    change pattern to :

    rx.SearchPattern = "(?mi-Us)Name=(.+)"
  3. Jean-Yves P

    Nov 7 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    or Kem would prefer Name=([\w|.]+)

  4. Jean-Yves P

    Nov 7 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    -image-

  5. Aditya N

    Nov 7 Pre-Release Testers, Xojo Pro

    Thanks, tested on ReGexRX is doing fine, how about assigning each of the matched pattern into undefined size string array?,
    above code is stopped at the line "result.Insert(counter, myMatch.SubExpressionString(counter))" with exception out of bound.

  6. Kem T

    Nov 7 Pre-Release Testers, Xojo Pro, XDC Speakers Answer New York

    It's an easy mistake to make (I did it myself initially) but you've confused SubExpressionCount with matches. If you have a pattern like "(a)(b)(c)" and use that on the text "abc", you will get a single match with SubExpressionCount = 4, SubExpressionString( 0 ) = "abc", SubExpressionString( 1 ) = "a", etc. If you need to get the next match of "abc", you have to call Search again.

    dim rx as new RegEx
    rx.SearchPattern = "some pattern"
    dim match as RegExMatch = rx.Search( myText )
    while match isa object
      // do something with match
      match = rx.Search // Get the next
    wend

    In your case, I'd using this pattern:

    ^ +Name=(.*)

    For each match, assign SubExpressionString( 1 ) to your array.

    To break that down, "^" is an anchor that matches the start of the line, " +" is one or more spaces, "Name=" is literal text, and "(.*)" will capture all the remaining text to the end of the line into group 1.

  7. Jean-Yves P

    Nov 7 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    @Kem T : first time I see you promote a "(.*)" !!!

  8. Kem T

    Nov 7 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    @Jean-YvesPochez or Kem would prefer Name=([\w|.]+)

    I'm not absolutist. :) I think ".*" is overused to be sure, but when you mean "anything at all till the end of the line", it's appropriate.

  9. Kem T

    Nov 7 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    Having said that, if the resulting match should only be word characters or a dot, I'd go with Jean-Yves' suggestion instead.

  10. Aditya N

    Nov 7 Pre-Release Testers, Xojo Pro

    Cool, seems working when using MsgBox, but still failed while assigning it into a undefined size one dimension array :-(
    Now the code based on Kem answer :

    'Prepare a regular expression object
    Dim myRegEx As RegEx
    Dim myMatch As RegExMatch
    Dim result() As String
    Dim counter As Integer
    Dim temp As Integer = 0 
    Dim tempCount As Integer = 0 
    myRegEx = New RegEx
    myRegEx.Options.TreatTargetAsOneLine = False
    myRegEx.SearchPattern = "^ +Name=(.*)"
    'Pop up all matches one by one
    myMatch = myRegEx.Search(txtAreaInput.Text)
    //tempCount = myMatch.SubExpressionCount + 1
    //Redim result(tempCount)
    //MsgBox(result.Ubound.ToText)
    while myMatch isa object
      // do something with match
      result(counter) = myMatch.SubExpressionString(1)
      counter = counter + 1
      MsgBox(myMatch.SubExpressionString(1))
      myMatch = myRegEx.Search // Get the next
    wend
    For temp = 0 To result.Ubound
      txtAreaResult.AppendText(result(temp))
    Next temp

    The code stopped at this line at first iteration of 4 iterations based on above example data :

      result(counter) = myMatch.SubExpressionString(1)
  11. Aditya N

    Nov 7 Pre-Release Testers, Xojo Pro

    Dohhh missread and forgot what array.append doing :-), now working fine with code

    result.Append(myMatch.SubExpressionString(1))

    Thanks @Kem Tekinay & @Jean-Yves Pochez !

or Sign Up to reply!