Regex matches bash variable
I am trying to change a bash script. The current script contains
print "<div class=\"title\">" $1 "</div>" Where $1 might look like this:
Apprentice Historian (Level 1) Historian (Level 4) Master Historian (Level 7) What I would like to do is add an image called a “base” value. I had something like this:
print "<div class=\"icon\"><imgsrc=\"icons\" $1 ".png\"></div><div class=\"title\">" $1 "</div>" However, in this case, I would like $1 return Historian . I thought I could use a regex to match $1 and keep only the part I need.
(Apprentice|Master)?\s(.*)\s(\(Level \d\)) I know that my regular expression is not quite there, ideally the student / master will be in his own group of matches and not tied to the base. And I do not know how to match the argument of $1 .
Using regex matching in bash:
for a in 'Apprentice Historian (Level 1)' 'Historian (Level 4)' 'Master Historian (Level 7)' ; do set "$a" echo " === $1 ===" [[ $1 =~ (Apprentice|Master)?' '?(.*)' ('Level' '[0-9]+')' ]] \ && echo ${BASH_REMATCH[${#BASH_REMATCH[@]}-1]} done The hard part is getting the correct member from BASH_REMATCH. Bash does not support non-capturing parentheses, so Historian is under 1 or 2. Fortunately, we know this is the last.
Based on "And I Don't Know How to Match the $ 1 Argument."
Did I understand you correctly if you ask if your regular expression is correct, but how to match the contents of your bash variable?
matched_text=$(echo $yourbashvariablecontainingthetext | sed 's/your_regex/backreference_etc/') $ yourbashvariablecontextthetext should be your $ 1