Reputation: 1118
Using VBScript or VBA I'm trying to write a REGEX Query to capture all Words between two specific string values.
Conditions: Multiline, Global
I'm trying to capture all words between the 'Logs:' and 'WORKSTATION#' Strings
Test String:
show access-log log brief
Logs:
main
streaming
ssl
cifs
mapi
im
p2p
WORKSTATION#(config)show
Expected Result:
main, streaming, ssl, cifs, mapi, im, p2p
My current non-working code:
Function GetLogList()
Dim strInput, objRegEx, oRegResults
Dim X, Y, Z
strInput = "show access-log log brief" & vbCrLf & _
"Logs:" & vbCrLf & _
"main" & vbCrLf & _
"streaming" & vbCrLf & _
"ssl" & vbCrLf & _
"cifs" & vbCrLf & _
"mapi" & vbCrLf & _
"im" & vbCrLf & _
"p2p" & vbCrLf & _
"WORKSTATION#(config)show"
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
objRegEx.Global = True
'Need Help with pattern
objRegEx.Pattern = ":\s*(\S*)\s*?WORKSTATION#"
Set oRegResults = objRegEx.Execute(strInput)
For X = 0 To oRegResults.Count - 1
Debug.Print oRegResults(X).Value
For Y = 0 To oRegResults(X).SubMatches.Count - 1
If oRegResults(X).SubMatches(Y) <> vbNullString Then
Debug.Print oRegResults(X).SubMatches(Y)
End If
Next
Next
End Function
I would greatly appreciate any help with my REGEX pattern to capture all words between the : and WORKSTATION#. Thank You!
EDIT:
Thanks for the suggestions. I had a laps and didn't think of this. I had thought my actual strings would have additional whitespace but it turns out the below works great capturing the whole string and splitting by vbCrLf. Thank You to everyone who helped.
Final Routine:
Function GetLogListArray(strInput)
Dim objRegEx, oRegResults
GetLogListArray = vbNullString
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
objRegEx.Global = True
objRegEx.Pattern = "Logs\:\s*([\S\s]*?)\s*?\S*?#"
Set oRegResults = objRegEx.Execute(strInput)
If oRegResults.Count = 0 Then Exit Function
If oRegResults(0).Submatches.Count = 0 Then Exit Function
GetLogListArray = Split(oRegResults(0).Submatches(0), vbCrLf)
End Function
Upvotes: 1
Views: 4280
Reputation: 210
Why not capture the whole list of words first then parse it?
objRegEx.Pattern = ":\s*([\S\s]*)\s?WORKSTATION#"
Then, your oRegResults would have 1 string containing all words that you can call split on (with delimiter being newline) to get words in an array.
Upvotes: 0
Reputation: 67968
^[\s\S]*?Logs:|WORKSTATION[\s\S]*$|\n
Try replace.Replace by ,
.See demo.
http://regex101.com/r/yR3mM3/39
Upvotes: 1
Reputation:
If you are trying to capture each word independently, its a two step process.
Get the full text between
Multiline, Global
while (
^Logs:([\S\s]*?)^WORKSTATION#
)
{
split on whitespace, the string returned in capture group 1.
}
Upvotes: 3