I want to parse strings similar to the following into separate variables using regular expressions from within Bash:
Category: entity;scheme=\"http://schemas
You can emulate non-matching groups in bash using a little bit of regexp magic:
_2__ _4__ _5__
[[ "fu@k" =~ ((.+)@|)((.+)/|)(.+) ]];
echo "${BASH_REMATCH[2]:--} ${BASH_REMATCH[4]:--} ${BASH_REMATCH[5]:--}"
# Output: fu - k
Characters @
and /
are parts of string we parse.
Regexp pipe |
is used for either left or right (empty) part matching.
For curious, ${VAR:-
is variable expansion with default value in case $VAR is empty.