基本正则expression式与扩展正则expression式的含义

我认为*意味着在基本或扩展正则expression式之前的零个或更多的字符或类。 为什么echo hello| grep '*llo' echo hello| grep '*llo'失败,但echo hello |egrep '*llo'成功了吗?

当使用grep / egrep / fgrep时,可以包含-o标志使grep只返回匹配的字符。 (如果你有一个很好的彩色terminal,你也可以尝试--color以便在返回的行中突出显示匹配。

 echo "that star " | grep -o '*count' echo "that star " | egrep -o '*count' echo "that star " | fgrep -o '*count' echo "that star counted" | grep -o '*count' echo "that star counted" | egrep -o '*count' ## returns "count" echo "that star counted" | fgrep -o '*count' echo "that star *counted" | grep -o '*count' ## returns "*count" echo "that star *counted" | egrep -o '*count' ## returns "count" echo "that star *counted" | fgrep -o '*count' ## returns "*count" 

没有评论的人什么都没有返回。

所以区别在于,旧的grep和fgrepparsing器,当他们没有看到一个字符或在星号之前设置时,select将其视为正常字符来匹配。 egrep将其视为无效或无效,并默默继续。

(另外需要注意的是,我有时使用pcregrep来获得perl regex的兼容性,当正则expression式以星号开头的时候,它实际上会抛出一个错误信息!)

http://www.regular-expressions.info/repeat.html

http://www.robelle.com/smugbook/regexpr.html

在正则expression式中,星号用于查找字符的模式,而不是进行查找。

换句话说,你应该说echo hello | grep 'llo*' echo hello | grep 'llo*'来查找'llo'或'lloooo'等来匹配模式中的更多字母,用圆括号括起来。 (llo)*会findllo,llollo等

我猜grep与*失败,因为它不是一个有效的正则expression式而egrep只是忽略了*。