VBScript Regex issue
I've had some serious issues with trying to get my Regex working properly trying to extract an UNC path. I've read through countless tutorials, guides and even tested my regex's in online regexp testers (where they seem to work), but I still can't get it to work in my code. I can, however, get it working in PHP for example.
I'm using PrimalScript to try to see what goes wrong, more on that later. Here's my current code which I'm using:
Dim WSHShell, strString, nrMatches, myMatches
Set WSHShell = CreateObject("WScript.Shell")
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
' myRegExp.Pattern = "^\\\\(.*?)+\\(.*)*\s...\\(.*)*$" <-- Returns 1 match, the whole String
' myRegExp.Pattern = "^\\\\(\w?)+\\(\w)*\s...\\(\w)*$" <-- Returns 0 matches
' myRegExp.Pattern = "^\\\\(.*?)+\\\(.*)*\s...\\\(.*)*$" <-- Gives Syntax Error
' myRegExp.Pattern = "^\\\\\\\\(.*?)+\\\(.*)*\s...\\\(.*)*$" <-- Gives Syntax Error
' myRegExp.Pattern = "^\\\\(.*)\\(.*)\s\.\.\.\\(.*)?$" <-- Returns 1 match, the whole String
myRegExp.Pattern = "^(.*)+\\(.*)+(\s\.\.\.\\(.*))?$" ' <-- Returns 1 match, the whole String
strString = "\\domain.subnet.net\share1 ...\开发者_如何学JAVAshare2"
Set myMatches = myRegExp.Execute(strString)
nrMatches = myMatches.Count
MsgBox "Found " & nrMatches & " Matches!", vbOKOnly, "Number of Matches"
For Each myMatch In myMatches
MsgBox "Value: " & myMatch.Value, vbOKOnly, "Found Match"
Next
WScript.Quit
The commented regex's are just a small portion of what I've tried, these are the ones I've had "most" sucess with.
One thing that caught my eye was that, while debugging in PrimalScript, it basically told me that the myMatches.Item = Invalid number of parameters Googling on it gave me nothing, though, but perhaps someone here knows what parameters Execute needs? I could provide a screenshot of it if necessary, just let me know.
Thanks, I'll appreciate any pointers or tips to help me get this going =]
I am not sure what you are expecting.
Do you want to result strString = "\\domain.subnet.net\share1 ...\share2"
in 2 matches? (Would ...\share2 be a valid path?)
If there are only paths in your string divided by whitespace, then you can try:
[^\s]+
see rubular
or
[\\\w.]+
see rubular
or if it has to start with \\
or .
(?<=\A|\s)(?:\\|\.)[^\s]+
see Regexr (because rubular does not support look behinds)
UPDATE:
According to your comments, I hope this will do what you want:
^((?:\\|\.)[^\s]+)\\[^\\\s]+\s+\.{3}([^\s]*)
Rubular
You will find the path till the last \
in the group 1 and the part following on the ...
in group 2. So to get your replacement you have just to concatenate group 1 and group 2.
Does something like this help?
http://regexlib.com/REDetails.aspx?regexp_id=2396
They seem to be suggesting
^[a-zA-Z]\:\.|^\\.
I also saw (?:<(\\[-\d\w\\s]+?)>)|(\\[-\d\w\]+) ina Google search.
精彩评论