简体   繁体   中英

Interesting grep match in bash

Can you explain why

This one gives $? = 1

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

and this one give $? = 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.

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. The end of line is preceded by a word character, so grep can't match an empty string as a word there.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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