Bash Regex验证

ukxgm1gy  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(93)

为什么下面的正则表达式工作:https://regex101.com/r/nI8xB8/1但在bash脚本中不起作用

^(feature|bugfix|refactor|release|hotfix|other)\/[a-z0-9._-]+\/[a-z0-9._-]+$

带测试字符串

feature/XXXX-23/xxxx-xxxx

但是feature/no-ref/xx-xxxx可以用
脚本

local_branch="$(git rev-parse --abbrev-ref HEAD)"

valid_branch_regex="^(feature|bugfix|refactor|release|hotfix|other)\/[a-z0-9._-]+\/[a-z0-9._-]+$"

message="\x1B[01;91m Some message. \x1B[0m"

if [[ ! $local_branch =~ $valid_branch_regex ]] 
then
    echo "$message"
    exit 1
 else 
    echo "\x1B[01;92m Your branch name is okay \x1B[0m"
fi
zengzsys

zengzsys1#

我让它与下面的正则表达式一起工作

valid_branch_regex="^(feature|bugfix|refactor|release|hotfix|other)\/[[:alnum:]._+-]+\/[[:alnum:]._+-]+$"

使用POSIX类::alnum:和排序规则也允许_-
参考:
https://en.wikibooks.org/wiki/Regular_Expressions/POSIX_Basic_Regular_Expressions

相关问题