sed-如何使用sed做正则表达式组 [英] sed - how to do regex groups using sed

查看:117
本文介绍了sed-如何使用sed做正则表达式组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,您是否可以像Java regex pattern/match/group一样使用sed做regex匹配组?

Is there anyway you can do regex match group using sed like java regex pattern/match/group?

如果我有类似

test-artifact-201251-balbal-0.1-SNAPSHOT.jar

我如何仅使用sed来获得如下结果:

how do I use sed just to get the result like:

test-artifact-0.1-SNASHOT.jar

我想知道sed是否允许您执行类似Java regex的操作,您定义的模式如下:

I am wondering does sed allow you to do something like java regex, you define the pattern like:

([a-z]*-[a-z]*-)([0-9]*-)([a-z]*-)([.]*SNAPSHOT.jar)

然后您可以将结果作为一个数组获得,例如:

and then you can get the results as an array like:

test-artifact-
201251-
balbal-
0.1-SNAPSHOT.jar

推荐答案

您必须对括号进行转义才能对表达式进行分组:

You have to escape parentheses to group expressions:

\([a-z]*-[a-z]*-\)\([0-9]*-\)\([a-z]*-\)\([.]*SNAPSHOT.jar\)

并与\1\2等一起使用.

编辑:还请注意,在SNAPSHOT之前,[.]将不匹配.内括号.为文字.应该是[0-9.-]*

EDIT: Also note just before SNAPSHOT that [.] will not match. Inside brackets . is literal. It should be [0-9.-]*

这篇关于sed-如何使用sed做正则表达式组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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