简体   繁体   English

bash中有趣的grep匹配

[英]Interesting grep match in bash

Can you explain why 你能解释为什么

This one gives $? 这个给$? = 1 = 1

echo "uus" | grep -w -o [0123456789]\*

and this one give $? 这个给$? = 0 = 0

echo "-uus" | grep -w -o [0123456789]\*

Your regular expression can match an empty string. 您的正则表达式可以匹配一个空字符串。 The -w flag means that any match must be preceded by beginning-of-line or a non-word character, and followed by end-of-line or a non-word character. -w标志意味着任何匹配都必须在行首或非单词字符之前,然后在行尾或非单词字符之后。

In the case of uus , the beginning of line is followed be a word character, so grep can't match an empty string as a word there. uus的情况下, uus的开头是单词字符,因此grep不能将空字符串作为单词匹配。 The end of line is preceded by a word character, so grep can't match an empty string as a word there. 该行的末尾带有单词字符,因此grep不能将一个空字符串作为单词匹配。

In the case of -uus , the beginning of line is followed by - , which is a non-word character, so grep can match the empty string as a word between the beginning of the line and the - character. -uus的情况下, -uus的开头是- ,这是一个非单词字符,因此grep可以将空字符串作为单词匹配到行的开头和-字符之间。

grep与'-'和'u'之间的零长度字匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM