正则表达式:加号和星号 [英] regex: plus sign vs asterisk
问题描述
星号或星号告诉引擎尝试匹配前一个令牌零次或多次。加号告诉引擎尝试匹配前一个令牌一次或多次。
基于该定义,我想知道为什么加号返回比星号更多的匹配。
echoABC ddd kkk DDD| grep -Eo[AZ] +
退货
echoABC ddd kkk DDD| grep -Eo[AZ] *
返回
ABC
据我所知,事实并非如此。使用GNU grep版本2.5.3,2.6.3,2.10和2.12,我得到:
$ echoABC ddd kkk DDD| grep -Eo[A-Z] +
ABC
DDD
$ echoABC ddd kkk DDD| grep -Eo[AZ] *
ABC
DDD
请加倍 - 检查你的第二个例子。如果你可以确认你只有一行输出,它可能是你的 grep
中的一个错误。如果您使用的是GNU grep
,那么 grep --version
的输出是什么?如果没有,您使用的是哪个操作系统,以及(如果您知道) grep
执行情况
更新:
我从源代码构建并安装了GNU grep 2.5.1(您使用的版本),并确认您的输出。它似乎是该版本 grep
中的一个错误,显然是在2.5.1a和2.5.3之间纠正的。 GNU grep 2.5.1大约12岁;你可以安装更新的版本吗?查看2.5.3中的 ChangeLog
,我怀疑这可能是修复: code> 2005-08-24 Charles Levert< charles_levert@gna.org>
* src / grep.c(print_line_middle):在空匹配的情况下,
取得最小进度并继续执行,而不是中止进程剩余部分的进程
,in如果还有即将到来的
非空比赛。
* tests / foad1.sh:为此添加两个测试。
* doc / grep.texi,doc / grep.1:记录这种行为,因为
- 一致匹配和--color是GNU扩展,它们是
,否则POSIX或其他标准。
即使您在使用的机器上没有完全访问权限,您仍然应该能够从 ftp://ftp.gnu.org/gnu/grep/
下载源压缩包并将其安装到您的主目录下(假设您的系统有一个工作编译器,相关工具)。
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]+"
returns
ABC DDD
echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]*"
returns ABC
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
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?
UPDATE :
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.
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屋!