Re: Regular expression matching fails with string RE
From:
Greg Wooledge
Subject:
Re: Regular expression matching fails with string RE
Date:
Wed, 17 Oct 2012 08:27:03 -0400
User-agent:
Mutt/1.4.2.3i
On Tue, Oct 16, 2012 at 02:18:40PM -0700, carson@taltos.org wrote:
> if [[ "foo1" =~ ".*([0-9])" ]]; then
As others have said, you must NOT quote the regular expression on the
right-hand side of the =~ operator. If you quote it, it becomes a
plain string, overriding the =~ operator entirely.
The best approach is to place the RE into a shell variable:
re=".*([0-9])"
if [[ foo1 =~ $re ]]; then
This avoids many issues, including changes in bash's behavior in the 3.x
versions.