C 用于解析函数命令行参数的正则表达式

C 用于解析函数命令行参数的正则表达式,c,regex,C,Regex,如何解析以下函数命令行: 分隔符(\s-\w\s)类似于-c或-d或-n C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d , 致: 谢谢。只需将-\w放入捕获组中,然后在re.split

如何解析以下函数命令行: 分隔符(\s-\w\s)类似于-c或-d或-n

C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d ,
致:


谢谢。

只需将
-\w
放入捕获组中,然后在
re.split
函数中使用此正则表达式即可。捕获组是必需的,以便它保留分隔符(即,仅捕获组中存在的字符)


如果您只想解析命令行参数,那么应该选中argparse libraryUse。没有必要重新发明这个轮子。谢谢你的快速回答。但是我用QT用C++,所以我需要一个ReGEXPUT。但是我在你的问题上看到了一个Python标签。抱歉,这是我的错。我是否需要你可以在下面测试它们:请将文本更改为:C:/my app/bin/Reader.dll-n Proc_20ms-C:/Users/Braun/Desktop/test.csv-t从csv文件的第一行继续模拟-j none-V errors and warnings(default)-d,使用regexp(\s(-\w)\s”)|(\s+)我错过了空白,然后参数是空白的错误,它测试了吗?仍然是不正确的。请测试:C//BIN /BIL/Reald.DLL-N PROPUR20MS-C:/用户/布劳恩/桌面/Test.CSV-T继续模拟从CSV文件的第一行- J NO-V错误和警告(默认)-D,
Match1:  C:/my app/bin/Reader.dll
Match2: -n
Match3: Proc_20ms
Match4: -c
Match5: C:/Users/Braun/Desktop/test.csv
Match6: -t
Match7: Continue the simulation from the first line of the csv-file 
Match8: -j
Match9: none
Match10: -V
Match11: errors and warnings (default)
Match12: -d
Match13: ,
>>> s = 'C:/my app/bin/Reader.dll -n Proc_20ms -c C:/Users/Braun/Desktop/test.csv -t Continue the simulation from the first line of the csv-file -j none -V errors and warnings (default) -d ,'
>>> for i in re.split(r'\s(-\w)\s', s):
        print(i)


C:/my app/bin/Reader.dll
-n
Proc_20ms
-c
C:/Users/Braun/Desktop/test.csv
-t
Continue the simulation from the first line of the csv-file
-j
none
-V
errors and warnings (default)
-d
,