# bash regex broken, won't recognize subexpressions



## steventhegood (Sep 27, 2008)

I'm horrified. /bin/bash 3.2.17 that came with a brand new iMac
does not recognize subexpressions in regular expressions.

This is recognized.

[[ happyFeet =~ 'happyFeet' ]] && echo recognized

This is NOT. Which makes it impossible to use the array BASH_REMATCH.

[[ happyFeet =~ 'happy(Feet)' ]] || echo not recognized

I can hardly believe this. I'd prefer to believe that I am doing something
wrong, but bash behaves properly on Linux.

Help?


----------



## ElDiabloConCaca (Sep 27, 2008)

All documentation seems to point to bash supporting exactly that:

http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/bash.1.html

Dropping the single quotes around the regex seems to work for me (I know, yikes!):

```
$ [[ happyFeet =~ 'happy(Feet)' ]] || echo not recognized
not recognized
$ [[ happyFeet =~ slappy(Feet) ]] || echo not recognized
not recognized
$ [[ happyFeet =~ happy(Foot) ]] || echo not recognized
not recognized
$ [[ happyFeet =~ happy(Feet) ]] || echo not recognized
$
```


----------



## steventhegood (Sep 27, 2008)

Yikes is right! You're right, though. It certainly doesn't work like this on my Linux
boxes. It's odd, and makes me fear for portability.

But thanks for checking and finding out that it's not TOTALLY broken.


----------



## tvuong (Mar 28, 2009)

Thanks for discovering this.  I puzzle on this for a long while why the script does not work on Mac.  But the problem with not having quote now is the shell may do wild card expansion when the expression contain shell wild card *, ?, [], ... and expand the expression differently depending on the current directory.   So you would not know when it may break


----------

