'Regex branch name pattern conditional
I am trying to get the branch name using regex and then use it in a case.
Does anyone know how it could work?
Possible names are:
- release/v1.1 -> release
- master -> master
- develop -> develop
BRANCH="release/v1.1";
#BRANCH="master";
#BRANCH="develop";
#branch_name=`expr "${BRANCH}" : '^\(release\)\/v[0-9]\.[0-9]$'`
branch_name=`expr "${BRANCH}" : '^\(master)|(release\)\/v[0-9]\.[0-9]$'`
echo $branch_name
Solution 1:[1]
You can also use sed and put every alternative in it's own group
The part ((release)/v[0-9]+(\.[0-9]+)? matches release followed by / and 1+ digits, optionally followed by . and 1+ digits.
In the replacement use group 2 and group 4.
for BRANCH in release/v1.1 master develop; do
sed -E 's~^((release)/v[0-9]+(\.[0-9]+)?|(develop|master))$~\2\4~' <<< "$BRANCH"
done
Output
release
master
develop
See the regex groups.
You could also use a bit shorter version of the pattern with awk, setting the field separator to / and when there is a match print field 1.
awk -F'/' '/^(release\/v[0-9]+(\.[0-9]+)?|develop|master)$/ {print $1}' <<< "$BRANCH"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
