3

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'

... but I would like to know how understand sed regex a little better.

Update:

I tried this ...

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

... as suggest but that outputs XML-Xerces-2.7.0-0.tar.gz

4

2 回答 2

6

你不能在 sed 中做非贪婪的正则表达式,但你可以做这样的事情:

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

它将捕获所有内容,直到找到 a-后跟[0-9].

于 2013-09-13T16:49:19.287 回答
3

当你在 bash 中时,你实际上并没有 sed:

shopt -s extglob
V='XML-Xerces-2.7.0-0.tar.gz'
echo "${V%%-+([0-9]).+([0-9])*}"
于 2013-09-13T17:00:50.027 回答