正则表达式:加号与星号 [英] regex: plus sign vs asterisk

查看:25
本文介绍了正则表达式:加号与星号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

星号或星号告诉引擎尝试匹配前面的标记零次或多次.加号告诉引擎尝试匹配前面的标记一次或多次.

The asterisk or star tells the engine to attempt to match the preceding token zero or more times. The plus tells the engine to attempt to match the preceding token once or more.

根据定义,我想知道为什么加号返回的匹配项多于星号.

Based on the definition, I was wondering why the plus sign returns more matches than the asterisk sign.

 echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]+"

返回

ABC DDD

 echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]*"

返回ABC

推荐答案

据我所知,没有.使用 GNU grep 版本 2.5.3、2.6.3、2.10 和 2.12,我得到:

As far as I can tell, it doesn't. With GNU grep versions 2.5.3, 2.6.3, 2.10, and 2.12, I get:

$ echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]+"
ABC
DDD
$ echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]*"
ABC
DDD

请仔细检查您的第二个示例.如果您可以确认只得到一行输出,则可能是您的 grep 中的错误.如果您使用 GNU grepgrep --version 的输出是什么?如果没有,您使用的是什么操作系统,以及(如果您知道)什么 grep 实现?

Please double-check your second example. If you can confirm that you get only one line of output, it might be a bug in your grep. If you're using GNU grep, what's the output of grep --version? If not, what OS are you using, and (if you know) what grep implementation?

更新:

我刚刚从源代码构建并安装了 GNU grep 2.5.1(您使用的版本),我确认了您的输出.这似乎是 grep 版本中的一个错误,显然在 2.5.1a 和 2.5.3 之间更正了.GNU grep 2.5.1 大约 12 岁了;你能安装一个新版本吗?查看 2.5.3 的 ChangeLog,我怀疑这可能是修复:

I just built and installed GNU grep 2.5.1 (the version you're using) from source, and I confirm your output. It appears to be a bug in that version of grep, apparently corrected between 2.5.1a and 2.5.3. GNU grep 2.5.1 is about 12 years old; can you install a newer version? Looking through the ChangeLog for 2.5.3, I suspect this may have been the fix:

2005-08-24  Charles Levert  <charles_levert@gna.org>

    * src/grep.c (print_line_middle): In case of an empty match,
      make minimal progress and continue instead of aborting process
      of the remainder of the line, in case there's still an upcoming
      non-empty match.
    * tests/foad1.sh: Add two tests for this.
    * doc/grep.texi, doc/grep.1: Document this behavior, since
      --only-matching and --color are GNU extensions which are
      otherwise unspecified by POSIX or other standards.

即使您没有对正在使用的机器的完全访问权限,您仍然应该能够从 ftp://ftp.gnu.org/gnu/grep/下载源 tarball code> 并将其安装在您的主目录下(假设您的系统具有可用的编译器和相关工具).

Even if you don't have full access on the machine you're using, you should still be able to download the source tarball from ftp://ftp.gnu.org/gnu/grep/ and install it under your home directory (assuming your system has a working compiler and associated tools).

这篇关于正则表达式:加号与星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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