gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:16:53 25 4
gpt4 key购买 nike

我如何解析以下函数命令行:分隔符是 (\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 ,

至:

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: ,

谢谢。

最佳答案

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

>>> 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
,

关于c - 用于解析函数命令行参数的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29194544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com