0

I am using bash 3.1.17(1)-release and script below. It doesn't show group match for input i.e. 4fwk2_dev 2fwk5_dev

echo $FWK
FWK_REGEX="/(\d{1,3})fwk(\d)_dev/"
if [[ $FWK =~ $FW_REGEX ]]; then
   echo ${BASH_REMATCH[0]}
fi

Output

4fwk2_dev 2fwk5_dev

I would expect it to match 4, and also 2 if I add echo ${BASH_REMATCH[1]}. And if I add g (global) to regex then hope it would process both 4fwk2_dev and 2fwk5_dev.

  • bash regular expressions do not use pcre syntax. Lose the slashes, and use [0-9] or [[:digit:]] in place of \d – glenn jackman Mar 16 '16 at 15:49
  • A note about bash regexes: the bash manual references regex(3) and that page references regex(7) and there you can read about POSIX extended regular expressions. – glenn jackman Mar 16 '16 at 17:33
0

This seems to work:

echo $FWK
FWK_REGEX="([0-9]{1,3})fwk([0-9])_dev"
if [[ $FWK =~ $FWK_REGEX ]]; then
   echo ${BASH_REMATCH[0]}
fi

I've replaced the regex and corrected the typo in the variable name.

However, I have Bash 4.3.42(1), so YMMV.

  • Thnx a lot , works for first word in line, looking for 'global' like option in docs to process every word in line – irom Mar 16 '16 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.