sed 中的非贪婪(不情愿)正则表达式匹配? [英] Non greedy (reluctant) regex matching in sed?

查看:86
本文介绍了sed 中的非贪婪(不情愿)正则表达式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sed 来清理 URL 行以仅提取域.

I'm trying to use sed to clean up lines of URLs to extract just the domain.

所以来自:

http://www.suepearson.co.uk/product/174/71/3816/

我想要:

http://www.suepearson.co.uk/

(有无斜杠都无所谓)

我试过了:

 sed 's|\(http:\/\/.*?\/\).*|\1|'

and(转义非贪婪量词)

and (escaping the non-greedy quantifier)

sed 's|\(http:\/\/.*\?\/\).*|\1|'

但我似乎无法让非贪婪量词 (?) 起作用,因此它最终总是匹配整个字符串.

but I can not seem to get the non-greedy quantifier (?) to work, so it always ends up matching the whole string.

推荐答案

无论是基本的还是扩展的 Posix/GNU 正则表达式都无法识别非贪婪量词;你需要一个稍后的正则表达式.幸运的是,这个上下文的 Perl 正则表达式很容易获得:

Neither basic nor extended Posix/GNU regex recognizes the non-greedy quantifier; you need a later regex. Fortunately, Perl regex for this context is pretty easy to get:

perl -pe 's|(http://.*?/).*|\1|'

这篇关于sed 中的非贪婪(不情愿)正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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