[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regex matching oddity (bug, or 'unexpected feature' or...?)
From: |
Greg Wooledge |
Subject: |
Re: Regex matching oddity (bug, or 'unexpected feature' or...?) |
Date: |
Thu, 28 May 2009 16:28:20 -0400 |
User-agent: |
Mutt/1.4.2.2i |
On Thu, May 28, 2009 at 12:58:10PM -0700, Linda Walsh wrote:
> operator caused a failure in matching. I.e.:
> if [[ "$Var"=~"+([:digit:])" ]] ; then ...
> worked.
No, this is incorrect. You need spaces around the =~ so that it's parsed
as three separate things, rather than one thing. Your code is being parsed
like:
if [[ "xxxx" ]]
which is always going to be true, because it's testing whether xxxx is
more than 0 characters long.
> But:
> if [[ "$Var" =~ "+([:digit:])" ]] ; then ...
> evaluates to 'FALSE', trace(-x) shows the eval as:
If you want the right hand side to be treated as a regular expression,
then you have two problems here:
1. You're using extglob syntax instead of regex(3) syntax as the man
page says you should use.
2. You've quoted the right hand side, which forces it to be treated as
a literal string instead of a regular expression.
What you probably wanted to write is:
regex='[:digit:]+'
if [[ $Var =~ $regex ]]; then ...
If you wanted to use the extglob syntax, then you should use = instead of
the =~ operator:
shopt -s extglob
glob='+([:digit:])'
if [[ $Var = $glob ]]; then ...
Hope this helps.