Reputation: 1253
I've got the following code and have looked at numerous examples but cannot figure out what I'm doing wrong. I know the regular expression works (see https://regex101.com/r/cB9hN1/1) - just not in my bash script. It's for a git update hook, but clearly I'm doing something wrong! Here's what I've got:
regex='ref:( |)([D]|[U])([E]|[S])(\d+)';
string="My commit ref: US2233556"
if [[ $string =~ $regex ]];
then
echo "[SUCCESS] Your message contains ref: for a Story or Defect."
exit 0
else
echo "[POLICY] Your message is not formatted correctly. Please include a \"ref: USXXXXX\" or \"ref: DEXXX\" in the commit message."
exit 1
fi
I'd appreciate any help! Thank you!
Upvotes: 2
Views: 61
Reputation: 626806
You should use [0-9]
instead of \d
and you can merge alternated character classes into single classes ([D]|[U]
= [DU]
):
regex='ref:( |)([DU])([ES])([0-9]+)';
See demo here
If you are not using capture groups, just remove them:
regex='ref: ?[DU][ES][0-9]+';
Here is another demo. Note that ( |)
can be written shorter as ( ?)
or ( )?
and this way it will cause less backtracking.
Upvotes: 1