Getting only the shell-pattern match by a single variable-expansion:
A bit late, but might be useful for someone else.
Example:
I constructed this to extract the Episode numbering from a TV-Series text.
(which is a shell-pattern, with unknown numbers)
The following code has some explaining comments.
#!/bin/bash
#This is for extended globbing
shopt -s extglob
#Getting only the Shell-patternmatch by variable-expansion
Var="Text before match_S02E12_Text after match"
#We only want the pattern from TV-Series Episode
MatchPattern="S[0-9][0-9]E[0-9][0-9]"
OnlyTheMatch="${Var//@(${Var//@($MatchPattern)*/}|${Var//*@($MatchPattern)/})/}"
echo "${OnlyTheMatch}" > Output_OnlyTheMatchPattern.txt
#Unset extended globbing
shopt -u extglob
#This does de following:
#We want to 'replace' the text before and the text after with 'nothing' ${Var//match/}
#(and by that remove them)
#But the ${Var//match/} can remove only one match,
#but we want to remove multiple (the text before AND after)
#To use multiple match-patterns we must put them in @(..|..) separated by vertical bars '|'
#The @ in @(pattern) means: Match exactly one occurrence of the pattern
#OnlyTheMatch="${Var//@(TextBeforeMatch|TextAfterMatch)/}"
#The Text before the match can be found by Var Expansion 'replace':
# ${Var//@($MatchPattern)*/}
#(Here the * matches everything after the MatchPattern, leaving only the text before the match)
#The Text after the match can also be found by Var Expansion 'replace':
# ${Var//*@($MatchPattern)/}
#(Here the * matches everything before the MatchPattern, leaving only the text after the match)
#And those variables can both be written in the @(..|..) separated by vertical bars '|'
#Together making the single variable-expansion to only get the matching pattern.
So, in case of the OP to "..keep the pattern and throw (away) the rest." :
MatchPattern="[A-Z]+([A-Z])-[0-9][0-9]+([0-9])"
Maybe this comes close to what you are looking for.