从字符串中提取可选值
所以,假设我有字符串:
aaa -Dprop=var Class arg
aaa Class arg
我想要一个单行代码(perl、sed、awk,没关系)可以提取 arg
和 var
(如果它在那里)。具体来说,我想返回
arg var
arg
以下适用于第一个:
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*(-Dprop=([a-z]*)).*Class (.*)|\3 \2|'
但因为需要 -Dprop=
,所以它显然不适用于第二个。
但是,如果我将该匹配设置为可选:
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*(-Dprop=([a-z]*))?.*?Class (.*)|\3 \2|'
它对第一个匹配不起作用,因为我相信两个 .* 是贪婪的限定符,并且首先匹配 -Dprop 。
如果我让它们变得非贪婪,它仍然不起作用,但我不确定为什么。
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*?(-Dprop=([a-z]*))?.*?Class (.*)|\3 \2|'
那么,首先,我可以使用什么正则表达式来正确匹配? (我知道我可以将它分成多个命令,但我宁愿只有一个)。
So, let's say I have the strings:
aaa -Dprop=var Class arg
aaa Class arg
I want a single one-liner (perl, sed, awk, doesn't matter) that can extract arg
and var
(if its there). Specifically, I'd like to return
arg var
arg
The following works for the first one:
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*(-Dprop=([a-z]*)).*Class (.*)|\3 \2|'
but because -Dprop=
is required, it obviously doesn't work the second one.
If, however, I make that match optional:
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*(-Dprop=([a-z]*))?.*?Class (.*)|\3 \2|'
it doesn't work for the first one because, I believe the two .*s are greedy qualifiers and match -Dprop first.
If I make them non-greedy, it still doesn't work, but I'm not sure why.
echo "aaa -Dprop=var Class arg" | perl -pe 's|.*?(-Dprop=([a-z]*))?.*?Class (.*)|\3 \2|'
So, first, what regex can I use that matches correctly? (I know I could split it into multiple commands, but I rather just have one).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许:
还要注意 \3 最好写成 $3 (警告编译指示会泄露)。
Perhaps:
Notice too that \3 is better written as $3 (which the warnings pragma would divulge).
这可能对你有用:
This might work for you: