1

为什么第一个和第二个变体不起作用,但第三个起作用

    #!/bin/sh
    #---------------------------------------------

    text="my:?text=this:one"
    if (echo $text | grep '^my\:\?text=this\:one') then
        echo "1"
    elif (echo $text | grep '^my:\?text=this:one') then
        echo "2"
    elif (echo $text | grep 'text=this:one') then
        echo "3"
    fi
4

4 回答 4

7

grep 默认不使用正则表达式,添加-E标志以启用扩展正则表达式。

编辑: grep 默认情况下不使用扩展正则表达式,并且grep -E通常使用别名来egrep更快地使用

于 2012-08-21T12:23:39.777 回答
3

使用Egrep扩展 grep 功能:

echo $text | egrep '^my\:\?text=this\:one'
于 2012-08-21T12:25:17.463 回答
1

因为:不是正则表达式中的特殊符号,不需要转义。

于 2012-08-21T12:23:12.180 回答
1

去掉问号前的反斜杠。它不被视为特殊字符grep。相反,添加反斜杠会为其添加特殊含义。

于 2012-08-21T12:24:49.220 回答