用于将可选参数匹配为字符串中的组的正则表达式
我正在尝试读取一个批处理脚本,其中包含命令的输入字符串和可选参数,例如
input = 'command -a field1 -b field2 -c field3'
选项参数由用户在文件中设置,我通过读取文件来获取字符串。如果包含该选项,我需要提取该字段。
我目前的正则表达式为:
expr = '^(?P<exec>[^\s]+) -m (?P<mode>[^\s]+) -d (?P<machine>[^\s]+) -p (?P<port>[^\s]+)'
m = re.match(expr, input)
当用户以相同的顺序包含所有选项时,正则表达式与字符串匹配并捕获组。这是一个示例输出。
{ 'exec': 'custom-script',
'mode': 'debug',
'machine': 'desk-123',
'port': '7905' }
但是,如果未包含任何选项,或者它们的顺序不同,则正则表达式将失败。如何修改我的正则表达式以在这两种情况下执行?
I am trying to read a batch script containing an input string of a command and optional parameters like
input = 'command -a field1 -b field2 -c field3'
The option parameters are set by a user in a file and I am obtaining the string by reading the file. If the option is included I would need to extract the field.
I currently have my regex as:
expr = '^(?P<exec>[^\s]+) -m (?P<mode>[^\s]+) -d (?P<machine>[^\s]+) -p (?P<port>[^\s]+)'
m = re.match(expr, input)
When the user includes all the options in the same order, the regex matches the string and the groups are captured. Here is a sample output.
{ 'exec': 'custom-script',
'mode': 'debug',
'machine': 'desk-123',
'port': '7905' }
However, if any option is not included, or if they are in different orders, the regex fails. How do I modify my regex to perform in these two cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Python 中,您可以使用内置的 optparse 或 argparse 模块来为您执行此操作,这样您就不必担心最终会失败的硬核正则表达式:-)
In Python you can use the built in optparse or argparse modules to do this for you so you don't have to fidget with hardcore regular expressions which will fail eventually :-)
在 python 中使用正则表达式解析命令行选项非常脆弱 - argparse 会让你处理您想要的所有参数,包括可选参数和位置参数,并提供丰富的功能来操作它们。它甚至会自动为您构建一个
-h
选项!例如,在您的情况下,您说您已向您提供输入,因此您可以执行以下操作:
此输出
args
将具有不同的字段作为属性,即args.mode
、args.machine
、args.port
parsing command line options with regular expressions in python is very fragile - argparse will let you handle all of the arguments you want, both optional and positional, and provides abundant faculties for manipulating them. It even automatically builds a
-h
option for you!For example, in your case you said you have the input supplied to you, so you could do the following:
The output from this,
args
, will have the different fields as attributes, i.e.,args.mode
,args.machine
,args.port
感谢 Nate,我使用了 optparse 而不是正则表达式,并使用了以下代码:
现在,我可以通过 args.mode、args.machine 等访问这些值。
Thanks to Nate, I used optparse instead of regexes and used the following code:
Now, I can access the values by args.mode, args.machine, etc.