expr字符串匹配 - 混合数字和字符?

问题描述:

当我输入expr字符串匹配 - 混合数字和字符?

$ expr match "can't find" 'c' 
$ 1 

然后我输入

$ expr match "234can't find" 'c' 
$ 0 

我想不通为什么?

man页的expr版本不是很清楚:

STRING : REGEXP 
      anchored pattern match of REGEXP in STRING 

match STRING REGEXP 
      same as STRING : REGEXP 

究竟什么不“挂靠”是什么意思? BSD版清除的东西了:

正则表达式锚定到 开始与一个隐含的``^“”的字符串。

所以expr match "234can't find" 'c'是相同的expr match "234can't find" '^c',由于您的字符串不开始c,匹配失败。


由于bash支持正则表达式匹配本身,你可以赞成

[[ "234can't find" =~ c ]] 
+0

放弃expr命令你救我,chepner。 – 2013-04-27 11:51:30