[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regular expression matching fails with string RE
From: |
John Kearney |
Subject: |
Re: Regular expression matching fails with string RE |
Date: |
Wed, 17 Oct 2012 04:17:56 +0200 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 |
Am 17.10.2012 03:13, schrieb Clark WANG:
> On Wed, Oct 17, 2012 at 5:18 AM, <carson@taltos.org> wrote:
>
>> Bash Version: 4.2
>> Patch Level: 37
>>
>> Description:
>>
>> bash -c 're=".*([0-9])"; if [[ "foo1" =~ ".*([0-9])" ]]; then echo
>> ${BASH_REMATCH[0]}; elif [[ "bar2" =~ $re ]]; then echo ${BASH_REMATCH[0]};
>> fi'
>>
>> This should output foo1. It instead outputs bar2, as the first match fails.
>>
>>
>> From bash's man page:
> [[ expression ]]
> ... ...
> An additional binary operator, =~, is available, with the
> same
> ... ...
> alphabetic characters. Any part of the pattern may be quoted
> to
> force it to be matched as a string. Substrings matched
> by
> ... ...
Drop the quotes on the regex
bash -c 're=".*([0-9])"; if [[ "foo1" =~ .*([0-9]) ]]; then echo
${BASH_REMATCH[0]}; elif [[ "bar2" =~ $re ]]; then echo ${BASH_REMATCH[0]};
fi'
outputs foo1