Reputation: 11671
I using bash and I have string (a commit message)
:sparkles: feat(xxx): this is a commit
and I want to divide it into variables sections:
emoji=:sparkles:
type=feat
scope=xxx
message=this is a commit
I try to use grep, but the regex is not return what I need (for example the "type") and anyway how to paste it into variables?
echo ":sparkles: feat(xxx): this is a commit" | grep "[((.*))]"
Upvotes: 0
Views: 253
Reputation: 88999
With bash version >= 3, a regex and an array:
x=":sparkles: feat(xxx): this is a commit"
[[ "$x" =~ ^(:.*:)\ (.*)\((.*)\):\ (.*)$ ]]
echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
echo "${BASH_REMATCH[3]}"
echo "${BASH_REMATCH[4]}"
Output:
:sparkles: feat xxx this is a commit
From man bash
:
BASH_REMATCH
: An array variable whose members are assigned by the =~ binary operator to the [[ conditional command. The element with index 0 is the portion of the string matching the entire regular expression. The element with index n is the portion of the string matching the nth parenthesized subexpression. This variable is read-only.
Upvotes: 5