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 ]]
放弃
expr
命令你救我,chepner。 – 2013-04-27 11:51:30