简体   繁体   中英

REGEX to Capture All Words Between Two Strings

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

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.  

}

^[\s\S]*?Logs:|WORKSTATION[\s\S]*$|\n

Try replace.Replace by , .See demo.

http://regex101.com/r/yR3mM3/39

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM