Link to home
Start Free TrialLog in
Avatar of peispud
peispudFlag for Canada

asked on

Regex - Capture everything between two strings.

Hi

 The code below does not work properly.  I am using Microsoft Access VBA.   When the code works properly,  the variable
 strAnswer  =  "Line 4:Line5" with the nonprinting characters preserved.

I am using the wrong pattern.  Also, I am not sure how to assign the regex result to a string.

I would appreciate your help.

Public Sub ExtractString()
    Dim str As String
    str = "Line 1." & vbCrLf
    str = str & "Line 2 :" & vbCrLf
    str = str & "Line 3 :" & vbCrLf
    
    str = str & "Line 4:" & vbCrLf
    str = str & "Line 5:" & vbCrLf
    
    str = str & "Line 6:" & vbCrLf
    str = str & "The End"
    
    Dim reg As New regExp
    reg.Pattern = "Line 4:\r(.*?)\rLine 6:"
     
    reg.Global = True
    reg.IgnoreCase = True
    Dim Matches As MatchCollection
    Set Matches = reg.Execute(str)
    
    Dim strAnswer As String
    strAnswer = reg.Execute(str)
End Sub

Open in new window


Thank you.
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
Public Sub ExtractString()
    Dim str As String
    str = "Line 1." & vbCrLf
    str = str & "Line 2 :" & vbCrLf
    str = str & "Line 3 :" & vbCrLf
    
    str = str & "Line 4:" & vbCrLf
    str = str & "Line 5:" & vbCrLf
    
    str = str & "Line 6:" & vbCrLf
    str = str & "The End"
    
    Dim reg As New regExp
    reg.Pattern = "Line 4:\r(.*?)\rLine 6:"
     
    reg.Global = True
    reg.IgnoreCase = True
    Dim Matches As MatchCollection
    Set Matches = reg.Execute(str)
    
    Dim strAnswer As String
    Dim Matches as Object
    set Matches = reg.Execute(str)
    strAnswer = Matches(0)
End Sub

Open in new window

Regards
Avatar of peispud

ASKER

Nope ... It did not work


Furthermore, strAnswer = Matches(0)                  generated the following error. "Runtime error 5.   Invalid procedure call or argument."
then try
Public Sub ExtractString()
    Dim str As String
    str = "Line 1." & vbCrLf
    str = str & "Line 2 :" & vbCrLf
    str = str & "Line 3 :" & vbCrLf
    
    str = str & "Line 4:" & vbCrLf
    str = str & "Line 5:" & vbCrLf
    
    str = str & "Line 6:" & vbCrLf
    str = str & "The End"
    
    Dim reg As New RegExp
    reg.Pattern = "Line 4:\r\n(.*?)\r\nLine 6:"
     
    reg.Global = True
    reg.IgnoreCase = True
    Dim Matches As MatchCollection
    Set Matches = reg.Execute(str)
    Dim strAnswer
    strAnswer = Matches(0).SubMatches(0)
End Sub

Open in new window

Avatar of peispud

ASKER

That worked.  But when I changed the pattern to

reg.Pattern = "Line 3:\r\n(.*?)\r\nLine 6:"

Open in new window


an error code is generated.  "Invalid procedure call or argument."

I would like to get all the text including non-printing characters.
then try
Line 3 :\r\n([\w\s]*)\r\nLine 6:

Open in new window

EDITED
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peispud

ASKER

I had to rem out the following line.
 reg.Multiline = True

Open in new window


After that,  it seems to work perfectly.
Avatar of peispud

ASKER

Much appreciated.  I get lots of data that is computer generated.   The data is always in the same place, so now I can develop a tool to sift out the data that I need.

Thank you.