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



我有这个字符串:

Process.exe /Switch=Value /Switch="C:Path with spacesfile.txt" wrong argument

我想捕捉这些部分:

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

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

这就是我正在尝试的:

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

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

此外,这不会像命令解释器(cmd.exe)首先解析的那样解析特殊的管道/重定向符号。相反,它会分离(解析)传递给某一语言的CreateProcess()的字符串。这个是C/C++,根据命令调用的语言,可能会有不同的解析方式。

使用的参考-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"rn]+|"[^"]+(?:"|$))+)|(?!^)[ t]+((?>[^ t"\rn]+|(?<!\)(?:\\)*"[^"\rn]*(?:\.[^"\rn]*)*"{1,2}|(?:\(?:\\)*")+|\+(?!"))+)|([^ trn]))
 #  "(?:^[ \t]*((?>[^ \t"\r\n]+|"[^"]+(?:"|$))+)|(?!^)[ \t]+((?>[^ \t"\\\r\n]+|(?<!\\)(?:\\\\)*"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"{1,2}|(?:\\(?:\\\\)*")+|\\+(?!"))+)|([^ \t\r\n]))"
 #  @"(?:^[ t]*((?>[^ t""rn]+|""[^""]+(?:""|$))+)|(?!^)[ t]+((?>[^ t""\rn]+|(?<!\)(?:\\)*""[^""\rn]*(?:\.[^""\rn]*)*""{1,2}|(?:\(?:\\)*"")+|\+(?!""))+)|([^ trn]))"
 (?:
      ^ [ t]* 
      (                             # (1 start), Command
           (?>
                [^ t"rn]+          
             |  
                " [^"]+ 
                (?: " | $ )
           )+
      )                             # (1 end)
   |  
      (?! ^ )
      [ t]+                        # Delimeter
      (                             # (2 start), Argument
           (?>
                [^ t"\rn]+ 
             |  
                (?<! \ )
                (?: \ \ )*
                " [^"\rn]* (?: \ . [^"\rn]* )*  "{1,2}
             |  
                (?:
                     \ (?: \ \ )*  " 
                )+
             |  
                \+ (?! " )
           )+
      )                             # (2 end)
   |  
      ( [^ trn] )                # (3), Bad character
 )

 #   #use strict;
 #   #use warnings;
 #   
 #   my $string;
 #   
 #   while ($string = <DATA>)
 #   {
 #       chomp($string);
 #       next if (length($string) == 0);
 #   
 #       print "nString To Parse:  '$string'n";
 #   
 #       while ( $string =~ 
 #          m~(?:^[ t]*((?>[^ t"rn]+|"[^"]+(?:"|$))+)|(?!^)[ t]+((?>[^ t"\rn]+|(?<!\)(?:\\)*"[^"\rn]*(?:\.[^"\rn]*)*"{1,2}|(?:\(?:\\)*")+|\+(?!"))+)|([^ trn]))~xg
 #             )
 #       {
 #          print "command:          $1n" if (defined $1);   
 #          print "argument:         $2n" if (defined $2);   
 #          print "bad char:         $3n" if (defined $3);   
 #       }
 #   }
 #   __DATA__
 #   F:"Program Files"Test"H "Arunthis.exe -arg
 #   "Console Application Template.vshost.exe" /Switch1=Value1 /Switch2="Value2"
 # 
 #   run-A  /Switch=Value /Switch="C:Path with spacesfile.txt" -arg
 #   run-B  Hotkey="Escape"
 #   run-C  Command="Dir /B "*.*"" Path=....
 #   
 #   run-01 CallMeIshmael
 #   run-02 "Call Me Ishmael"
 #   run-03 Cal"l Me I"shmael
 #   run-04 CallMe"Ishmael 
 #   run-05 "CallMe"Ishmael"
 #   run-06 "Call Me Ishmael\" 
 #   run-07 "CallMe\"Ishmael" 
 #   run-08 a\b
 #   run-09 ""Call Me Ishmael"" 
 #   run-10 "C:TEST A\" 
 #   run-11 ""C:TEST A\""
 #   run-12 "a b c"  d  e
 #   run-13 "ab"c"  "\"  d
 #   run-14 a\b d"e f"g h
 #   run-15 a\"b c d
 #   run-16 a\\"b c" d e
 #   run-17 "a b c""
 #   run-18 """CallMeIshmael"""  b  c
 #   run-19 """Call Me Ishmael"""
 #   run-20 """"Call Me Ishmael"" b c
 #   run-21 """CallMeIshmael"""
 #   run-22 """Call Me Ishmael"""
 #   run-23 "CallMeIshmael"
 #   run-24 """"Call me Ishmael""""
 #   run-25 """"Call Me Ishmael""
 #   run-26 ""Call Me Ishmael""

输出>>

 String To Parse:  ' F:"Program Files"Test"H "Arunthis.exe -arg'
 command:          F:"Program Files"Test"H "Arunthis.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 spacesfile.txt"
 -arg'
 command:          run-A
 argument:         /Switch=Value
 argument:         /Switch="C:Path with spacesfile.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 "a b c"  d  e'
 command:          run-12
 argument:         "a b c"
 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\b d"e f"g h'
 command:          run-14
 argument:         a\b
 argument:         d"e f"g
 argument:         h
 String To Parse:  ' run-15 a\"b c d'
 command:          run-15
 argument:         a\"b
 argument:         c
 argument:         d
 String To Parse:  ' run-16 a\\"b c" d e'
 command:          run-16
 argument:         a\\"b c"
 argument:         d
 argument:         e
 String To Parse:  ' run-17 "a b c""'
 command:          run-17
 argument:         "a b c""
 String To Parse:  ' run-18 """CallMeIshmael"""  b  c'
 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"" b c'
 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 spacesnotepad.exe"""
 Dim myMatches As MatchCollection
 Dim MyRegEx As New Regex("([^/]+""|[^/s]+)")
 myMatches = MyRegEx.Matches(DebugArguments)
 For Each match In myMatches
 MsgBox(match.Value)
 Next

最新更新