gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:26:30 26 4
gpt4 key购买 nike

当我输入时

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

然后我输入

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

我不明白为什么?

最佳答案

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

STRING : REGEXP
anchored pattern match of REGEXP in STRING

match STRING REGEXP
same as STRING : REGEXP

“锚定”到底是什么意思? BSD 版本解决了问题:

The regular expression is anchored to the beginning of the string with an implicit ``^''.

所以 expr match "234can't find"'c'expr match "234can't find"'^c' 相同,因为你的字符串不c 开始,匹配失败。


由于 bash 本身支持正则表达式匹配,您可以放弃 expr 命令而使用

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

关于regex - expr 字符串匹配-混合数字和字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com