VBScript正则表达式问题

时间:2011-05-05 21:04:25

标签: regex vbscript

我遇到了一些严重的问题,试图让我的正则表达式正常工作以尝试提取UNC路径。我已阅读了无数的教程,指南,甚至在在线regexp测试人员(他们似乎工作的地方)中测试了我的正则表达式,但我仍然无法在我的代码中使用它。但是,我可以在PHP中使用它。

我正在使用PrimalScript试图查看出现了什么问题,稍后会详细介绍。这是我正在使用的当前代码:

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

评论的正则表达式只是我尝试过的一小部分,这些是我“最”成功的。

引起我注意的一件事是,在PrimalScript中进行调试时,它基本上告诉我myMatches.Item =参数数量无效 谷歌搜索它没有给我什么,但也许这里有人知道执行需要什么参数? 如果有必要,我可以提供它的截图,请告诉我。

谢谢,我会感谢任何指示或提示,以帮助我实现这一目标=]

2 个答案:

答案 0 :(得分:1)

这样的事情有帮助吗?

http://regexlib.com/REDetails.aspx?regexp_id=2396

他们似乎在暗示

^ [A-ZA-Z] \:\ | ^ \\

我也看到了 (?:?≤(\\ [ - \ d \ W \\ S] +)&GT;)|(\\ [ - \ d \ W \] +) 在Google搜索中。

答案 1 :(得分:1)

我不确定你在期待什么。

你想在2场比赛中得到strString = "\\domain.subnet.net\share1 ...\share2"吗? (将... \ share2成为有效路径吗?)

如果您的字符串中只有路径除以空格,那么您可以尝试:

[^\s]+请参阅rubular

[\\\w.]+请参阅rubular

或者必须以\\.

开头

(?<=\A|\s)(?:\\|\.)[^\s]+请参阅Regexr(因为rubular不支持外观)

<强>更新

根据你的评论,我希望这会做你想要的:

^((?:\\|\.)[^\s]+)\\[^\\\s]+\s+\.{3}([^\s]*)

Rubular

您将找到组1中最后一个\的路径以及组2中...后面的部分。因此,要获得替换,您只需要连接组1和组2