简体   繁体   English

使用正则表达式解析命令和参数?

[英]Parsing command and arguments with a regex?

I have a small Ruby CLI which should take a command and some arguments. 我有一个小的Ruby CLI,应该带有一个命令和一些参数。

For example, I should be able to do: 例如,我应该能够:

attribute value

or: 要么:

attribute (value1, value2)

or: 要么:

attribute (value1, value2) and attribute2 (value3, value4)

or any combination. 或任何组合。

I'd like to get back something like: 我想找回类似的东西:

[['attribute', ['value1', 'value2']], ['attribute2', ['value3', 'value4']]]

I came up with this regex 我想出了这个正则表达式

(\b[a-z_]{1}\w+)\s{1}\((.*?)\)

which gets me partially there, in that it matches the attribute and both the values (as a whole, like [['attribute', 'value1, value2']] , but it doesn't match something like attribute value . 这使我部分地到达了那里,因为它匹配属性和两个值(作为一个整体,例如[['attribute', 'value1, value2']] ,但是不匹配attribute value

I dont know how extensive of input you require, but something like this might do the trick. 我不知道您需要多少输入,但是像这样的事情可能会解决问题。 It works similarly to the shell getOpts. 它的工作原理与shell getOpts类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM