简体   繁体   English

正则表达式用于匹配cli选项

[英]Regexp for matching cli options

I need to refactor my regexp to match the bold part in this string: 我需要重构我的正则表达式以匹配此字符串中的粗体部分:

foo --(bar)( )(foo bar) --foo bar foo foo- (bar)()(foo bar)--foo bar foo

Capture groups are parenthesized. 捕获组带有括号。 I can't seem to figure out how to make it match both "--bar foo bar"" and "--bar="foo bar". 我似乎无法弄清楚如何使其与“ --bar foo bar”“和” --bar =“ foo bar”匹配。 If it is matching against unquoted input, eg --bar foo bar", it should stop at the next occurrence of "--" or at the end of the string. 如果它与未加引号的输入匹配,例如--bar foo bar“,则应在下一个出现的”-“或字符串末尾停止。

This is the regexp I'm working with: 这是我正在使用的正则表达式:

([^="\'\s]+?)(=| ?)((?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')+)

Thank you for being so lovingly kind to help me with this issue. 感谢您如此乐于帮助我解决此问题。

Maybe it's something like this you need. 也许您需要这样的东西。 With use of a delimiting lookahead . 使用定界的前瞻

 /--(\w+)[\s="]+(.*?)(?:"|(?=\s*--|$))/s

Have a try at regex101 and grab the captures if needed. 尝试regex101并在需要时捕获捕获。

\\w stands for "word character". \\ w代表“文字字符”。 It always matches the ASCII characters [A-Za-z0-9_] . 它始终与ASCII字符[A-Za-z0-9_]匹配。
\\s stands for "whitespace character"... it includes [ \\t\\r\\n\\f] \\ s代表“空白字符” ...包括[ \\t\\r\\n\\f]
www.regular-expressions.info/shorthand.html www.regular-expressions.info/shorthand.html

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

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