正则表达式以匹配进程参数

我有这个字符串:

Process.exe /Switch=Value /Switch="C:\Path with spaces\file.txt" wrong argument 

我想捕捉这些部分:

 · 1st Match: Process.exe · 2nd Match: /Switch=Value · 3rd Match: /Switch="C:\Path with spaces\file.txt" · 4th Match: wrong · 5th Match: argument 

正则表达式需要用于通用(具有真实参数的过程),不仅适用于这种情况。

这就是我想要的:

 Dim DebugArguments As String = "HotkeyMaker.exe /Hotkey=Escape /run=""c:\folder with spaces\notepad.exe""" For Each match As Match In Regex.Matches(DebugArguments, "([^\s]+[^""])") MsgBox(match.Value) Next match 

编辑
在这里,您可以使用正则表达式来解析C / C ++命令行参数。 每个论点都需要
进一步子处理以解决参数级别上的转义和双引号。

此外,这不会解析特殊的管道/重定向符号,因为它首先由命令解释程序(cmd.exe)解析。 相反,它将传递给语言的CreateProcess()的字符串分开(解析)。 这个是C / C ++,可能会有不同的解析,具体取决于Command调用的语言

使用的参考 – http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESREPH

Edit2添加了命令解析部分

  # CmdLineParser_Cpp.rxf # ------------------------------------------------------------------------------- # Reference used - "How Command Line Parameters Are Parsed" C/C++ cmdline parsing: # http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESREPH # ------------------------------------------------------------------------------- # (?:^[ \t]*((?>[^ \t"\r\n]+|"[^"]+(?:"|$))+)|(?!^)[ \t]+((?>[^ \t"\\\r\n]+|(?[^ \\t\"\\r\\n]+|\"[^\"]+(?:\"|$))+)|(?!^)[ \\t]+((?>[^ \\t\"\\\\\\r\\n]+|(?[^ \t""\r\n]+|""[^""]+(?:""|$))+)|(?!^)[ \t]+((?>[^ \t""\\\r\n]+|(? [^ \t"\r\n]+ | " [^"]+ (?: " | $ ) )+ ) # (1 end) | (?! ^ ) [ \t]+ # Delimeter ( # (2 start), Argument (?> [^ \t"\\\r\n]+ | (?) # { # chomp($string); # next if (length($string) == 0); # # print "\nString To Parse: '$string'\n"; # # while ( $string =~ # m~(?:^[ \t]*((?>[^ \t"\r\n]+|"[^"]+(?:"|$))+)|(?!^)[ \t]+((?>[^ \t"\\\r\n]+|(? 

输出>>

  String To Parse: ' F:\"Program Files"\Test\"H "A\runthis.exe -arg' command: F:\"Program Files"\Test\"H "A\runthis.exe argument: -arg String To Parse: ' "Console Application Template.vshost.exe" /Switch1=Value1 /S witch2="Value2"' command: "Console Application Template.vshost.exe" argument: /Switch1=Value1 argument: /Switch2="Value2" String To Parse: ' run-A /Switch=Value /Switch="C:\Path with spaces\file.txt" -arg' command: run-A argument: /Switch=Value argument: /Switch="C:\Path with spaces\file.txt" argument: -arg String To Parse: ' run-B Hotkey="Escape"' command: run-B argument: Hotkey="Escape" String To Parse: ' run-C Command="Dir /B "*.*"" Path=..\..\' command: run-C argument: Command="Dir /B "*.*"" argument: Path=..\..\ String To Parse: ' ' String To Parse: ' run-01 CallMeIshmael' command: run-01 argument: CallMeIshmael String To Parse: ' run-02 "Call Me Ishmael"' command: run-02 argument: "Call Me Ishmael" String To Parse: ' run-03 Cal"l Me I"shmael' command: run-03 argument: Cal"l Me I"shmael String To Parse: ' run-04 CallMe\"Ishmael ' command: run-04 argument: CallMe\"Ishmael String To Parse: ' run-05 "CallMe\"Ishmael"' command: run-05 argument: "CallMe\"Ishmael" String To Parse: ' run-06 "Call Me Ishmael\\" ' command: run-06 argument: "Call Me Ishmael\\" String To Parse: ' run-07 "CallMe\\\"Ishmael" ' command: run-07 argument: "CallMe\\\"Ishmael" String To Parse: ' run-08 a\\\b' command: run-08 argument: a\\\b String To Parse: ' run-09 "\"Call Me Ishmael\"" ' command: run-09 argument: "\"Call Me Ishmael\"" String To Parse: ' run-10 "C:\TEST A\\" ' command: run-10 argument: "C:\TEST A\\" String To Parse: ' run-11 "\"C:\TEST A\\\""' command: run-11 argument: "\"C:\TEST A\\\"" String To Parse: ' run-12 "abc" de' command: run-12 argument: "abc" argument: d argument: e String To Parse: ' run-13 "ab\"c" "\\" d' command: run-13 argument: "ab\"c" argument: "\\" argument: d String To Parse: ' run-14 a\\\bd"ef"gh' command: run-14 argument: a\\\b argument: d"ef"g argument: h String To Parse: ' run-15 a\\\"bcd' command: run-15 argument: a\\\"b argument: c argument: d String To Parse: ' run-16 a\\\\"bc" de' command: run-16 argument: a\\\\"bc" argument: d argument: e String To Parse: ' run-17 "abc""' command: run-17 argument: "abc"" String To Parse: ' run-18 """CallMeIshmael""" bc' command: run-18 argument: """CallMeIshmael""" argument: b argument: c String To Parse: ' run-19 """Call Me Ishmael"""' command: run-19 argument: """Call argument: Me argument: Ishmael""" String To Parse: ' run-20 """"Call Me Ishmael"" bc' command: run-20 argument: """"Call Me Ishmael"" argument: b argument: c String To Parse: ' run-21 """CallMeIshmael"""' command: run-21 argument: """CallMeIshmael""" String To Parse: ' run-22 """Call Me Ishmael"""' command: run-22 argument: """Call argument: Me argument: Ishmael""" String To Parse: ' run-23 \"CallMeIshmael\"' command: run-23 argument: \"CallMeIshmael\" String To Parse: ' run-24 """"Call me Ishmael""""' command: run-24 argument: """"Call me Ishmael"""" String To Parse: ' run-25 """"Call Me Ishmael""' command: run-25 argument: """"Call Me Ishmael"" String To Parse: ' run-26 "\"Call Me Ishmael\""' command: run-26 argument: "\"Call Me Ishmael\"" 

尝试这样的事情。

  Dim DebugArguments As String = "HotkeyMaker.exe /Hotkey=""Escape"" /run=""c:\folder with spaces\notepad.exe""" Dim myMatches As MatchCollection Dim MyRegEx As New Regex("([^\/]+""|[^/\s]+)") myMatches = MyRegEx.Matches(DebugArguments) For Each match In myMatches MsgBox(match.Value) Next