egrep 使用通配符匹配表达式

egrep matching expressions with wildcard

经过一些练习,卡在了这个:

egrep "s*as" states.txt

## Alaska
## Arkansas
## Kansas
## Massachusetts
## Nebraska

我能理解为什么它选择阿肯色州、堪萨斯州、马萨诸塞州,但为什么选择阿拉斯加? "s*as"应该是第一个"s",不是吗?我是否遗漏了一些明显的东西?对不起,如果答案很明显,我不明白。 grep 给出相同的结果。

在您拨打下面的 egrep 时:

egrep "s*as" states.txt

数量s*表示匹配s次或更多次。因此,Alaska 匹配,因为它包含 as。如果您打算匹配 s,后跟任何单个字符,然后是 as 然后使用点:

egrep "s.as" states.txt

请注意,文件名通配符和正则表达式之间存在差异。

* 在正则表达式中,引用 GNU Grep manual:

The preceding item will be matched zero or more times


* 在文件名通配符中,引用 Bash Reference Manual

Matches any string, including the null string