开发者

expr match problem in shell

In an sh shell script I wrote the following:

opr=+  
echo `expr match "$opr开发者_JAVA技巧" '[+\-x/]'`  

but I get this error when ran:

expr: syntax error  

What am I doing wrong? I get the same error when I make opr equal to - and / .

Another interesting thing I found is when I wrote this:

opr=a  
echo `expr match "$opr" '[+\-x/]'`  

it returns this:

1  

This means that it matched the string "a" to one of +, -, x, and /. But that makes no sense!


First case: +

+ has a special meaning to expr:

   + TOKEN
          interpret TOKEN as a string, even if it is a
          keyword like `match' or an operator like `/'

Second case: a

your regexp is a range operation, matching characters from + to x, which includes most alnums. To make the - be matched literally in a charclass, it must be the first or last character; backslashing it doesn't work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜