Vb.net 正则表达式正在阻止该程序

Vb.net 正则表达式正在阻止该程序,vb.net,regex,Vb.net,Regex,我有下一个正则表达式 Dim origen As String = " /c /p:""c:\mis doc umentos\mis imagenes construida\archivo.txt"" /cid:45 423 /z:65 /a:23 /m:39 /t:45rt " Dim str As String = "(^|\s)/p:""\w:(\\(\w+[\s]*\w+)+)+\\\w+.\w+""(\s|$)" Dim ar As Integer Dim getfile

我有下一个正则表达式

Dim origen As String = "  /c /p:""c:\mis doc umentos\mis imagenes construida\archivo.txt"" /cid:45    423 /z:65 /a:23  /m:39 /t:45rt "

Dim str As String = "(^|\s)/p:""\w:(\\(\w+[\s]*\w+)+)+\\\w+.\w+""(\s|$)"
Dim ar As Integer

Dim getfile As New Regex(str)
Dim mgetfile As MatchCollection = getfile.Matches(origen)
ar = mgetfile.Count
当我对它进行评估时,它工作了,并得到了
/p:“c:\mis doc umentos\mis imagenes construida\archivo.txt”
,这基本上是一个文件的路径

但是如果我把origen字符串改为

Dim origen As String = "  /c /p:""c:\mis doc umentos\mis imagenes construida\archivo.txt""/cid:45    423 /z:65 /a:23  /m:39 /t:45rt "
检查文件末尾是否后跟“/cid:45”,这会使de模式无效,但程序不是获得mgetfile.count=0,而是阻塞,如果进行调试,则会获得
属性评估失败。

您总是知道开头和结尾有两个双引号吗?如果是这样,就做:

(^|\s)/p:""(.*?)""(.*$)

您能否将整个表达式整理为:

str = "/p:"".*?"""

程序挂起的原因是

正则表达式的
(\w+\s*\w++
\w+
部分允许如此多的排列,以致正则表达式引擎陷入一个近乎无限的循环中。RegexBuddy的调试器在1000000个步骤后退出

只有当模式无法成功匹配时才会发生这种情况,从而促使正则表达式引擎尝试模式允许的任何和所有其他排列。通常,包含重复量词的重复组是危险的

真正的要求是什么?要匹配只包含字母、数字、下划线和反斜杠的路径?或者只是引号之间的字符串?也许你可以解释一下

在此之前,我建议如下:

"(?<=^|\s)/p:""\w:(\\[\w\s]++)+\.\w+""(?=\s|$)"

是最好最快的方法。

非常感谢发生的一切。。非常好的解决问题的指导…哇,非常感谢解决了这个问题,我只是在beginning处添加(^ |\s),并在末尾添加(\s+|$),以确保当右字符串后面的o之前有东西时,它不会包含有效的模式。最后的模式是(^ |\s+)/p:“.*”(\s+|$),谢谢大家的评论!!!我学到的是“保持简单!!!!!!!”是的,特别是对于正则表达式,越简单越好。
"(?<=^|\s)/p:""[^""]+""(?=\s|$)"