Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex VB.NET从字符串中提取文件路径_Regex_Vb.net - Fatal编程技术网

Regex VB.NET从字符串中提取文件路径

Regex VB.NET从字符串中提取文件路径,regex,vb.net,Regex,Vb.net,我不太擅长正则表达式,我需要以下任务的帮助: 在VB.NET中,我需要从随机给定的字符串处理命令行参数中提取一个或多个文件路径。例如: 我需要: “C:\ProgramFiles(x86)\Microsoft Office\Office15\WINWORD.EXE”/n“C:\Users\administrator\Documents\test.docx”/o”“ 提取以下内容的数组: C:\Program Files (x86)\Microsoft Office\Office15\WINWOR

我不太擅长正则表达式,我需要以下任务的帮助:

在VB.NET中,我需要从随机给定的字符串处理命令行参数中提取一个或多个文件路径。例如:

我需要:

“C:\ProgramFiles(x86)\Microsoft Office\Office15\WINWORD.EXE”/n
“C:\Users\administrator\Documents\test.docx”/o”“

提取以下内容的数组:

C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE

C:\Users\administrator\Documents\test.docx
试图在这里搜索,但没有帮到我。谢谢你的帮助

编辑:基于响应,我创建了这段代码。但它只提取第一条路径,而不是第二条路径:

编辑2:基于第二个响应(标记为正确),我现在编辑了我的代码和它的工作状态

´Public Function GetProcessOpenedFiles(ByVal p As Process) As List(Of String)
    Dim newlst As New List(Of String)

    Dim strCmdArgs As String = GetProcessCommandLine(p)
    Dim strExePath As String = GetProcessExePath(p).ToLower

    Dim expression As String = """(.+?)"""
    Dim mc As MatchCollection = Regex.Matches(strCmdArgs, expression)

    For Each m As Match In mc
        Dim strMatch As String = m.ToString.ToLower
        strMatch = Mid(strMatch, 2)
        strMatch = strMatch.Remove(strMatch.Length - 1)

        If strExePath <> strMatch Then
            If IO.File.Exists(strMatch) AndAlso newlst.Contains(strMatch) = False Then
                newlst.Add(strMatch)
            End If
        End If
    Next

    Return newlst
End Function´
公共函数GetProcessOpenedFile(ByVal p作为进程)作为列表(字符串)
Dim newlst作为新列表(字符串)
Dim strCmdArgs As String=GetProcessCommandLine(p)
Dim strExePath As String=GetProcessExePath(p).ToLower
Dim表达式为字符串=“(.+?)”“”
Dim mc As MatchCollection=Regex.Matches(strCmdArgs,表达式)
对于mc中的每个m As匹配
Dim strMatch As String=m.ToString.ToLower
标准格式=中间格式(标准格式,2)
strMatch=strMatch.移除(strMatch.长度-1)
如果是strExePath stratch,则
如果IO.File.Exists(stratch)和also newlst.Contains(stratch)=False,则
新增(标准格式)
如果结束
如果结束
下一个
返回newlst
结束函数´

抓取从开头的
到下一个
字符的所有内容

^"([^"]+)"

完成了。

您可以使用以下正则表达式:

"(.+?)"

MATCH 1
1.  [1-61]  `C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE`
MATCH 2
1.  [67-109]    `C:\Users\administrator\Documents\test.docx`

到目前为止您尝试了什么?您尝试过System.IO.Path函数吗?非常有助于获取路径信息。它甚至不必是文件路径。您可以将其用于URL等。感谢您的建议。我知道IO.Path类,但它(AFAIK)不包含任何可以从随机给定的文本中检索字符串(路径)列表的功能。此外,这里最好的正则表达式是在“”符号中封装的路径上工作,如果没有封装,函数将失败并且无法检索路径。它的工作方式与我提到的不完全相同,请参见编辑的问题。是的,此正则表达式比前一个更好-工作良好!谢谢