I have an array of strings where the elements have different colors and I want to filter the strings that have the brown color.
#!/bin/bash
cafeRegex='^\e[0;33m*{*}\e[0m$'
arrayColores=($(echo -e "\e[0;33m3tia01\e[0m \e[0;37mabuela32\e[0m 2Yo3 \e[1;37mabuelo21\e[0m
\e[0;32m49hijo1\e[0m \e[0;36m8papa\e[0m \e[0;33m33mama11\e[0m"))
echo "El array: ${arrayColores[*]}"
for familiar in ${arrayColores[*]}
do
if [[ $familiar =~ $cafeRegex ]]; then
echo "Los familiares cafes son: $familiar"
fi
done
The output I have is this:
It shows me the array with its colors but the sentence to filter the brown color is not executed, I suspect the regular expression.
The problem is in the regular expression you define:
^\e[0;33m*{*}\e[0m$
. This does not match, in regular expression terms, any of the strings you provide.I understand that what you want to look for is something like:
\e[0;33m3tia01\e[0m
\e[0;33m33mama11\e[0m
But note that this includes characters like "" or "[" which cannot be set as-is, but must be escaped. Also, you mention some
*
I understand that to refer to any character, but its use is incorrect: it*
indicates quantity, while it is.
what we use to match any character.Therefore, the expression should be like this:
This will match the strings that...
Therefore, your complete script would be: