peispud
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.
Thank you.
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
Thank you.
ASKER
Nope ... It did not work
Furthermore, strAnswer = Matches(0) generated the following error. "Runtime error 5. Invalid procedure call or argument."
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
ASKER
That worked. But when I changed the pattern to
an error code is generated. "Invalid procedure call or argument."
I would like to get all the text including non-printing characters.
reg.Pattern = "Line 3:\r\n(.*?)\r\nLine 6:"
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:
EDITED
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to rem out the following line.
After that, it seems to work perfectly.
reg.Multiline = True
After that, it seems to work perfectly.
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.
Thank you.
pls try
Open in new window
Regards