普通防爆pression匹配工艺参数 [英] Regular Expression to match process arguments

查看:170
本文介绍了普通防爆pression匹配工艺参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串:

  Process.exe /开关=价值/开关=C:用空格\路径\ file.txt的错误说法
 

我想捕捉这些部分:

 ·第一比赛:Process.exe
·第2比赛:/开关=值
·第三节比赛:/开关=C:用空格\路径\ file.txt的
·第四比赛:错
·第5比赛:参数
 

常规前pression需要是仿制的使用(与实际参数的过程),不仅可以适用于这种情况。

这就是我试着:

 昏暗DebugArguments的String =
    HotkeyMaker.exe /热键=退出/运行=C:\文件夹中有空格\ NOTEPAD.EXE

对于每场比赛由于比赛在Regex.Matches(DebugArguments,([^ \ s] + [^]))
    MSGBOX(match.Value)
下一场比赛
 

解决方案

修改
在这里,你走了,一个正则表达式解析C / C ++命令行参数。每个参数都需要被
进一步细分处理,以解决逃逸和双引号的参数级别。

此外,这并不解析为先的命令除preTER(CMD.EXE)解析专用管/重定向符号。相反,这种分离(解析)传递给CreateProcess的()为语言的字符串。这一个是C / C ++,可能会被解析有所不同,这取决于语言的由命令调用。

用于参考 - http://www.daviddeley.com/autohotkey/parameters /parameters.htm#WINCRULESREPH

EDIT2 添加命令解析部分

 #CmdLineParser_Cpp.rxf
 #------------------------------------------------- ------------------------------
 #参考使用 - 如何命令行参数解析C / C ++ CMDLINE解析:
 #http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESREPH
 #------------------------------------------------- ------------------------------

 ?#(:^ [\ t] *((&GT; [^ \ t的\ r \ n]的+ |[^] +(?:| $))+)|(^)?! \吨] +((大于?[^ \t"\\\r\n]+|(?<!\\)(?:\\\\)*"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"{1,2}|(?:\\(?:\\\\)*")+|\\+(?!"))+)|([^ \ t \ r \ n])的)
 #?(:^ [\\ T] *((&GT; [^ \\吨\\\ \\  -  [R N] + | \[^ \] +(?:\| $)) ?!?+)|(^)[\\吨] +((大于[^ \\t\"\\\\\\r\\n]+|(?<!\\\\)(?:\\\\\\\\)*\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"{1,2}|(?:\\\\(?:\\\\\\\\)*\")+|\\\\+(?!\"))+)|([^ \\ \\吨\\  -  [R N]))
 #@(:^ [\ t] *((&GT; [^ \ t?\ r \ n]的+ |[^] +(?:| $))+)| (?!^)[\吨] +((大于?[^ \t""\\\r\n]+|(?<!\\)(?:\\\\)*""[^""\\\r\n]*(?:\\.[^""\\\r\n]*)*""{1,2}|(?:\\(?:\\\\)*"")+|\\+(?!""))+)|([^ \ t \ r \ n])的)

 (?:
      ^ [\ t] *
      (#(1日开始),命令
           ?(大于
                [^ \ t的\ r \ n]的+
             |
                [^] +
                (?:| $)
           )+
      )#(1月底)
   |
      (?!^)
      [\ t] +#的Delimeter
      (#(2启动),参数
           ?(大于
                [^ \ t的\\\ r \ n]的+
             |
                (?&LT;!\\)
                (?:\\)*
                [^\\\ r \ n]的*(?:\\ [^\\\ r \ n]的*。*){1,2}
             |
                (?:
                     \\(?:\\)*
                )+
             |
                \\ +(?!)
           )+
      )#(2月底)
   |
      ([^ \ t \ r \ n])的#(3),人品不好
 )


 ##使用严格的;
 ##使用警告;
 #
 #我$串;
 #
 #而($字符串=&lt;数据&GT;)
 #{
 #格格($字符串);
 #Next如果(长度($字符串)== 0);
 #
 #打印\ nString解析:$字符串'\ N的;
 #
 #而($字符串=〜
 ?#M〜(:^ [\ t] *((&GT; [^ \ t的\ r \ n]的+ |[^] +(?:| $))+)|(^?! )[\吨] +((大于?[^ \t"\\\r\n]+|(?<!\\)(?:\\\\)*"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"{1,2}|(?:\\(?:\\\\)*")+|\\+(?!"))+)|([^ \ t \ r \ n])的)〜XG
 #)
 #{
 #打印命令:$ 1 \ N如果(定义$ 1);
 #打印的说法:$ 2 \ N如果(定义$ 2);
 #打印坏字符:$ 3 \ N如果(自定义$ 3);
 #}
 #}
 #   __数据__
 #F:\程序文件\测试\HA \ runthis.exe -arg
 #控制台应用程序Template.vshost.exe/交换机1 =值1 /交换机2 =值2
 #
 #运行A /交换机=价值/交换机=C:用空格\ file.txt的\路径-arg
 #运行-B热键=逍遥游
 #运行C命令=DIR / B*。*,路径= .. \ .. \
 #
 #运行01 CallMeIshmael
 #运行-02叫我以实玛利
 #运行03校准L我我shmael
 #运行-04的CallMe \以实玛利
 #运行-05的CallMe \以实玛利
 #运行-06叫我以实玛利\\
 #运行-07的CallMe \\\以实玛利
 #运行08一\\\ b
 #运行-09\叫我以实玛利\
 #运行10C:\试验A \\
 #运行-11\C:\试验A \\\
 #运行-12A B CD E
 #运行-13AB \C,\\D
 #运行14 \\\ B D的e FG H
 #运行15 \\\B C D
 #运行16 \\\\B CD E
 #运行-17A B C
 #运行-18CallMeIshmaelB C
 #运行-19叫我以实玛利
 #运行20叫我以实玛利B C
 #运行-21CallMeIshmael
 #运行-22叫我以实玛利
 #运行-23 \CallMeIshmael \
 #运行24叫我以实玛利
 #运行25叫我以实玛利
 #运行-26\叫我以实玛利\
 

输出>>

 的字符串进行解析:F:\程序文件\测试\HA \ runthis.exe -arg
 命令:F:\程序文件\测试\HA \ runthis.exe
 参数:精氨酸

 字符串解析:控制台应用程序Template.vshost.exe/交换机1 =值1 / S
 witch2 =值2
 命令:控制台应用程序Template.vshost.exe
 参数:/交换机1 =值1
 参数:/开关2 =值2

 字符串解析:运行A /交换机=价值/交换机=C:用空格\路径\ file.txt的
 精氨酸
 命令:运行-A
 参数:/开关=值
 参数:/开关=C:用空格\路径\ file.txt的
 参数:精氨酸

 字符串解析:运行-B热键=逍遥游
 命令:运行-B
 参数:热键=逍遥游

 字符串解析:运行C命令=DIR / B*,路径= .. \ .. \
 命令:运行-C
 参数:命令=DIR / B* *。
 参数:路径= .. \ .. \

 字符串解析:''

 字符串解析:运行01 CallMeIshmael
 命令:运行01
 参数:CallMeIshmael

 字符串解析:运行-02叫我以实玛利
 命令:运行02
 说法:叫我以实玛利

 字符串解析:运行03校准L我我shmael
 命令:运行03
 参数:CALL我我shmael

 字符串解析:运行-04的CallMe \以实玛利
 命令:运行04
 参数:的CallMe \以实玛利

 字符串解析:运行-05的CallMe \以实玛利
 命令:运行05
 说法:的CallMe \以实玛利

 字符串解析:运行-06叫我以实玛利\\'
 命令:运行06
 说法:叫我以实玛利\\

 字符串解析:运行-07的CallMe \\\以实玛利
 命令:运行07
 说法:的CallMe \\\以实玛利

 字符串解析:运行08一\\\ B'
 命令:运行08
 参数:一个\\\ b

 字符串解析:运行-09\叫我以实玛利\
 命令:运行09
 说法:\叫我以实玛利\

 字符串解析:运行-10C:\试验A \\'
 命令:运行10
 说法:C:\试验A \\

 字符串解析:运行-11\C:\试验A \\\
 命令:运行11
 说法:\C:\试验A \\\

 字符串解析:运行-12A B CD E'
 命令:运行12
 说法:A B C
 参数:D
 说法:电子

 字符串解析:运行-13AB \C,\\D
 命令:运行13
 说法:AB \C
 论据:         \\
 参数:D

 字符串解析:运行14 \\\ B D的e FG H'
 命令:运行14
 参数:一个\\\ b
 参数:D电子网G
 参数:H

 字符串解析:运行15 \\\B C D'
 命令:运行15
 参数:一个\\\B
 参数:c
 参数:D

 字符串解析:运行16 \\\\B CD E'
 命令:运行16
 参数:一个\\\\B C
 参数:D
 说法:电子

 字符串解析:运行-17A B C
 命令:运行17
 说法:A B C

 字符串解析:运行-18CallMeIshmaelBC
 命令:运行18
 说法:CallMeIshmael
 参数:乙
 参数:c

 字符串解析:运行-19叫我以实玛利
 命令:运行19
 说法:呼叫
 说法:我
 参数:以实玛利

 字符串解析:运行-20叫我以实玛利BC
 命令:运行20
 说法:叫我以实玛利
 参数:乙
 参数:c

 字符串解析:运行-21CallMeIshmael
 命令:运行21
 说法:CallMeIshmael

 字符串解析:运行-22叫我以实玛利
 命令:运行22
 说法:呼叫
 说法:我
 参数:以实玛利

 字符串解析:运行-23 \CallMeIshmael \'
 命令:运行23
 参数:\CallMeIshmael \

 字符串解析:运行-24叫我以实玛利
 命令:运行24
 说法:叫我以实玛利

 字符串解析:运行-25叫我以实玛利
 命令:运行25
 说法:叫我以实玛利

 字符串解析:运行-26\叫我以实玛利\
 命令:运行26
 说法:\叫我以实玛利\
 

I have this string:

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

I would like to capture these parts:

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

The regular expression need to be for generic use (a process with real arguments), not only be adapted for this case.

This is what Im trying:

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

解决方案

Edit
Here you go, a regex to parse C/C++ command line arguments. Each argument needs to be
further sub-processed to resolve escapes and double quotes on the argument level.

Further, this does not parse special pipe/redirection symbols as first parsed by the command interpreter (cmd.exe). Instead this separates(parses) the string passed to the CreateProcess() for a language. This one is C/C++ and might be parsed differently, depending on the language invoked by the Command.

Reference used - http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESREPH

Edit2 Added Command parse part

 # 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]+|(?<!\\)(?:\\\\)*"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"{1,2}|(?:\\(?:\\\\)*")+|\\+(?!"))+)|([^ \t\r\n]))
 #  "(?:^[ \\t]*((?>[^ \\t\"\\r\\n]+|\"[^\"]+(?:\"|$))+)|(?!^)[ \\t]+((?>[^ \\t\"\\\\\\r\\n]+|(?<!\\\\)(?:\\\\\\\\)*\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"{1,2}|(?:\\\\(?:\\\\\\\\)*\")+|\\\\+(?!\"))+)|([^ \\t\\r\\n]))"
 #  @"(?:^[ \t]*((?>[^ \t""\r\n]+|""[^""]+(?:""|$))+)|(?!^)[ \t]+((?>[^ \t""\\\r\n]+|(?<!\\)(?:\\\\)*""[^""\\\r\n]*(?:\\.[^""\\\r\n]*)*""{1,2}|(?:\\(?:\\\\)*"")+|\\+(?!""))+)|([^ \t\r\n]))"

 (?:
      ^ [ \t]* 
      (                             # (1 start), Command
           (?>
                [^ \t"\r\n]+          
             |  
                " [^"]+ 
                (?: " | $ )
           )+
      )                             # (1 end)
   |  
      (?! ^ )
      [ \t]+                        # Delimeter
      (                             # (2 start), Argument
           (?>
                [^ \t"\\\r\n]+ 
             |  
                (?<! \\ )
                (?: \\ \\ )*
                " [^"\\\r\n]* (?: \\ . [^"\\\r\n]* )*  "{1,2}
             |  
                (?:
                     \\ (?: \\ \\ )*  " 
                )+
             |  
                \\+ (?! " )
           )+
      )                             # (2 end)
   |  
      ( [^ \t\r\n] )                # (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"\r\n]+|"[^"]+(?:"|$))+)|(?!^)[ \t]+((?>[^ \t"\\\r\n]+|(?<!\\)(?:\\\\)*"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"{1,2}|(?:\\(?:\\\\)*")+|\\+(?!"))+)|([^ \t\r\n]))~xg
 #             )
 #       {
 #          print "command:          $1\n" if (defined $1);   
 #          print "argument:         $2\n" if (defined $2);   
 #          print "bad char:         $3\n" if (defined $3);   
 #       }
 #   }
 #   __DATA__
 #   F:\"Program Files"\Test\"H "A\runthis.exe -arg
 #   "Console Application Template.vshost.exe" /Switch1=Value1 /Switch2="Value2"
 # 
 #   run-A  /Switch=Value /Switch="C:\Path with spaces\file.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\""

Output >>

 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 "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\""

这篇关于普通防爆pression匹配工艺参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆