Avatar of peispud
peispud
Flag 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.
Regular Expressions

Avatar of undefined
Last Comment
peispud

8/22/2022 - Mon
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
peispud

ASKER
Nope ... It did not work


Furthermore, strAnswer = Matches(0)                  generated the following error. "Runtime error 5.   Invalid procedure call or argument."
Rgonzo1971

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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.
Rgonzo1971

then try
Line 3 :\r\n([\w\s]*)\r\nLine 6:

Open in new window

EDITED
ASKER CERTIFIED SOLUTION
Rgonzo1971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
peispud

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

Open in new window


After that,  it seems to work perfectly.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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.