I'm looking for a cleaner solution if one exists. I'm highlighting Spanish verb endings. The regex I came up with seems to work but I can't help but be curious if there is a better way.
Here is what I came up with: (it's super long and I don't know how to prevent it from wrapping)
hilite_tail () { local WORD=$1 local COLOR=$2 PFX=$(echo $WORD | perl -pe 's/(.*)(ste(?=$)|e(?<!ste)(?=$)|es(?=$ +)|is(?<!(eis|ste))(?=$)|mos(?=$)|n(?<!ron)(?=$)|o(?=$)|ó(?=$)|.ron(?= +$)|s(?<!(eis|.is|mos))(?=$)|steis(?=$)|é(?<!ué)(?=$)|í(?=$)|ué(?=$))/ +$1/g') SFX=$(echo $WORD | perl -pe 's/(.*)(ste(?=$)|e(?<!ste)(?=$)|es(?=$ +)|is(?<!(eis|ste))(?=$)|mos(?=$)|n(?<!ron)(?=$)|o(?=$)|ó(?=$)|.ron(?= +$)|s(?<!(eis|.is|mos))(?=$)|steis(?=$)|é(?<!ué)(?=$)|í(?=$)|ué(?=$))/ +$2/g') [[ $PFX != $SFX ]] && echo "${CYAN_FG}$PFX${COLOR}$SFX${RESET}" || + echo "${CYAN_FG}$WORD${RESET}" }
Using the advice I've now simplified:
hilite_tail () { local WORD=$1 local COLOR=$2 PFX=$(echo $WORD | perl -pe 's/(.*)(ste|(?<!st)e|es|is(?<!(eis|ste +))|mos|(?<!ro)n|o|ó|.ron|s(?<!(.is|mos))|steis|(?<!u)é|í|ué)$/$1/g') SFX=$(echo $WORD | perl -pe 's/(.*)(ste|(?<!st)e|es|is(?<!(eis|ste +))|mos|(?<!ro)n|o|ó|.ron|s(?<!(.is|mos))|steis|(?<!u)é|í|ué)$/$2/g') [[ $PFX != $SFX ]] && echo "${CYAN_FG}$PFX${COLOR}$SFX${RESET}" || + echo "${CYAN_FG}$WORD${RESET}" }
Thanks
In reply to Looking for a cleaner regex by k-man
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |