Issue
Why is the below regex working on: https://regex101.com/r/nI8xB8/1 But does not work in a bash script
^(feature|bugfix|refactor|release|hotfix|other)\/[a-z0-9._-]+\/[a-z0-9._-]+$
With test string
feature/XXXX-23/xxxx-xxxx
But feature/no-ref/xx-xxxx
works
Script
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
Solution
I got it to work with the below Regex
valid_branch_regex="^(feature|bugfix|refactor|release|hotfix|other)\/[[:alnum:]._+-]+\/[[:alnum:]._+-]+$"
Using the POSIX class: :alnum:
and collation to also allow _
and -
Refs:
https://en.wikibooks.org/wiki/Regular_Expressions/POSIX_Basic_Regular_Expressions
Answered By - Thabo Answer Checked By - David Marino (WPSolving Volunteer)