如何使SED做非贪婪匹配? [英] How to make sed do non-greedy match?

查看:3417
本文介绍了如何使SED做非贪婪匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何拿出正确的正则表达式为我的bash命令行。
下面是我在做什么:

I cannot seem to figure out how to come up with the correct regex for my bash command line. Here's what I am doing:

echo "XML-Xerces-2.7.0-0.tar.gz" | sed -e's/^\(.*\)-[0-9].*/\1/g'

这给我的......输出

This gives me the output of ...

XML-Xerces-2.7.0

...但想我需要的是输出为...

... but want I need is the output to be ...

XML-Xerces

...我想我能做到这一点...

... I guess I could do this ...

 echo "XML-Xerces-2.7.0-0.tar.gz" | sed -e's/^\(.*\)-[0-9].*/\1/g' | sed -e's/^\(.*\)-[0-9].*/\1/g'

...但我想知道如何理解 SED 正则表达式更好一点。

更新:

我想这...

echo "XML-Xerces-2.7.0-0.tar.gz" | sed -e's/^\([^-]*\)-[0-9].*/\1/g'

...作为建议但输出 XML-的Xerces-2.7.0-0.tar.gz

推荐答案

您不能做非贪婪的正则表达式的SED,但你可以做这样的事情,而不是:

You can't do non greedy regex in sed, but you can do something like this instead:

echo "XML-Xerces-2.7.0-0.tar.gz" | sed -e 's/^\(\([^-]\|-[^0-9]\)*\).*/\1/g'

这将捕捉一切,直到它找到一个 - 然后按 [0-9]

这篇关于如何使SED做非贪婪匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆