提问人:Thabo 提问时间:10/13/2023 最后编辑:Thabo 更新时间:10/13/2023 访问量:75
Bash 正则表达式验证
Bash Regex validation
问:
为什么以下正则表达式在以下方面起作用: 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
答:
1赞
Thabo
10/13/2023
#1
我让它与下面的正则表达式一起使用
valid_branch_regex="^(feature|bugfix|refactor|release|hotfix|other)\/[[:alnum:]._+-]+\/[[:alnum:]._+-]+$"
使用 POSIX 类:和排序规则也允许和:alnum:
_
-
裁判:
https://en.wikibooks.org/wiki/Regular_Expressions/POSIX_Basic_Regular_Expressions
评论
^
$
XXXX
nocasematch
A-Z
A-Z
0
[[ 'feature/XXXX-23/xxxx-xxxx' =~ ^(feature|bugfix|refactor|release|hotfix|other)\/[A-Za-z0-9._-]+\/[a-z0-9._-]+$ ]]
sed
sed
|
shopt -s nocasematch
valid_branch_regex='^(feature|bugfix|refactor|release|hotfix|other)\/[a-z0-9._-]+\/[a-z0-9._-]+$'