2

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 ...\share2"

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 =]

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@Stema another clarification: The shares could be multiple deep as well, which means if the string i "\\domain.subnet.net\share1\share2 ...\share3", I want to extract share 3 and replace share 2 with it. In other words, the purpose is to map (in this case) \\domain.subnet.net\share1*share2* and \\domain.subnet.net\share1*share3*
Hey @Stema, from what I've understood, VBScript doesn't allow for look behinds (See regular-expressions.info/vbscript.html) either - that might be what is causing this? Also, your second Regexr suggestion throws me a "Syntax error in regular expression". To Clarify my intentions, I need to reassemble the two shares into just that. The three concurrent dots (...) indicates that everything up until the last folder should be the same. I figured it'd be a lot easier to try to use a regular expression to extract the data, and the manipulate it. Perhaps the best way is to do it in steps?
@Daniel, sorry I didn't verified that, reading VB... I was thinking of .net which would have supported look behinds. Then try this here Rubular (?:^|\s)((?:\\|\.)[^\s]+). The look behind was only to avoid that this will be in the result. With this regex the leading space is also matched, but I put your path into the first capturing group instead. I replaced also the \A that is also not supported by VBScript.
@Exodus, so I added another solution, hope I get closer.
@Stema Unfortunately, none of your suggestions worked. The smaller one threw an exception (Expected ')' in regular expression). The first one gave only one match; the whole string. Also in Rebular it misses Share2 in the second example? I really appreciate the help by the way! :)
|
1

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.

1 Comment

Hi @mikeY, that got me the matches \domain, \share1 and \share2. Close, but not there yet. I'll try to modify your suggestion to see if it works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.