grep 与正则表达式:除非我添加断言,否则空格不匹配 [英] grep with regexp: whitespace doesn't match unless I add an assertion

查看:10
本文介绍了grep 与正则表达式:除非我添加断言,否则空格不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GNU grep 2.5.4 on bash 4.1.5(1) on Ubuntu 10.04

GNU grep 2.5.4 on bash 4.1.5(1) on Ubuntu 10.04

这匹配

$ echo "this is a     line" | grep 'a[[:space:]]+line'
this is a     line

但这没有

$ echo "this is a     line" | grep 'as+line'

但这也很匹配

$ echo "this is a     line" | grep 'as+line'
this is a     line

我不明白为什么#2 不匹配(而#1 匹配)并且#3 也显示匹配.这里有什么区别?

I don't understand why #2 does not match (whereas # 1 does) and #3 also shows a match. Whats the difference here?

推荐答案

查看您的 grep 联机帮助页.Perl 添加了许多原始规范中没有的正则表达式扩展.然而,由于它们被证明非常有用,许多程序都采用了它们.

Take a look at your grep manpage. Perl added a lot of regular expression extensions that weren't in the original specification. However, because they proved so useful, many programs adopted them.

不幸的是,grep 有时会停留在过去,因为您想确保您的 grep 命令与旧版本的 grep 保持兼容.

Unfortunately, grep is sometimes stuck in the past because you want to make sure your grep command remains compatible with older versions of grep.

有些系统有带有一些扩展的egrep.其他人允许您使用 grep -E 来获取它们.还有一些人有一个 grep -P 允许你使用 Perl 扩展.我相信 Linux 系统的 grep 命令可以使用在大多数 Unix 系统中不可用的 -P 扩展名,除非有人用 GNU 版本替换了 grep.较新版本的 Mac OS X 也支持 -P 开关,但不支持旧版本.

Some systems have egrep with some extensions. Others allow you to use grep -E to get them. Still others have a grep -P that allows you to use Perl extensions. I believe Linux systems' grep command can use the -P extension which is not available in most Unix systems unless someone has replaced the grep with the GNU version. Newer versions of Mac OS X also support the -P switch, but not older versions.

这篇关于grep 与正则表达式:除非我添加断言,否则空格不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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