Cross-version regular expression...

2011-04-13 Thread Ray Van Dolson
I'm trying to get regular expression matching working on both bash v3
as well as v4.

The following works on v4 (under RHEL6), but not under bash 3.00.15
(under RHEL4):

[[ "6Server" =~ ([0-9]+)(.*) ]]

This works on 3.00.15, but not under RHEL6's bash (4.1.2):

[[ "6Server" =~ '([0-9]+)(.*)' ]]

The prior actually throws a syntax error, so I can't even code around
the syntax based on evaluation of $BASH_VERSINFO.

Any thoughts on how to do this in a portable way?

Thanks,
Ray



Re: Cross-version regular expression...

2011-04-13 Thread Ray Van Dolson
On Wed, Apr 13, 2011 at 12:28:39PM -0700, Greg Wooledge wrote:
> On Wed, Apr 13, 2011 at 11:19:22AM -0700, Ray Van Dolson wrote:
> > I'm trying to get regular expression matching working on both bash v3
> > as well as v4.
> 
> Put the RE in a variable.
> 
> > The following works on v4 (under RHEL6), but not under bash 3.00.15
> > (under RHEL4):
> > 
> > [[ "6Server" =~ ([0-9]+)(.*) ]]
> 
> Put the RE in a variable.
> 
> > This works on 3.00.15, but not under RHEL6's bash (4.1.2):
> > 
> > [[ "6Server" =~ '([0-9]+)(.*)' ]]
> 
> Put the RE in a variable.  Don't quote the right hand side.  If you quote
> the right hand side, it becomes == matching instead of =~ matching.
> 
> re='([0-9]+)(.*)'
> [[ 6Server =~ $re ]]
> 

Thanks Greg!