我在(t|b|bug_|task_|)1234使用bash正则表达式捕获此格式的字符串中的数字时遇到问题.以下不起作用:
[[ $current_branch =~ ^(t|b|bug_|task_|)([0-9]+) ]]
Run Code Online (Sandbox Code Playgroud)
但是一旦我把它改成这样的东西:
[[ $current_branch =~ ^(t|b|bug_|task_)([0-9]+) ]]
Run Code Online (Sandbox Code Playgroud)
它有效,但当然是错误的,因为它没有涵盖没有前缀的情况.我知道在这种情况下我能做到
[[ $current_branch =~ ^(t|b|bug_|task_)?([0-9]+) ]]
Run Code Online (Sandbox Code Playgroud)
并获得相同的结果,但我想知道为什么第二个例子不起作用.例如,正则表达式似乎在Ruby中工作正常.
(这是GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11),OSX Lion)