bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Regular expressions with word boundaries no longer working in bash


From: Chet Ramey
Subject: Re: Regular expressions with word boundaries no longer working in bash
Date: Sat, 18 Dec 2010 22:44:09 -0500
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100802 Lightning/1.0b2 Thunderbird/3.1.2

On 12/17/10 12:00 PM, Harry Leitzell wrote:

> Bash Version: 4.1
> Patch Level: 5
> Release Status: release
> 
> Description:
> 
> Doing string matching based on word boundaries no longer works:
> 
> $ bash --version
> GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
> Copyright (C) 2005 Free Software Foundation, Inc.
> 
> $ cat test.sh
> input="foocombo foo foo bar bar foo bar some stuff foo" # the input
> expected="foocombo foo bar some stuff" # this what we expect
> for i in address@hidden; do
>     [[ ! address@hidden =~ \\b"${i}"\\b ]] && old=( address@hidden $i )
> done
> for i in address@hidden; do
>     [[ ! address@hidden =~ \b${i}\b ]] && new=( address@hidden $i )
> done
> echo "input:    $input"
> echo "expected: $expected"
> echo "old code: ${old[*]}"
> echo "new code: ${new[*]}"
> 
> $ bash test.sh
> input:    foocombo foo foo bar bar foo bar some stuff foo
> expected: foocombo foo bar some stuff
> old code: foocombo foo bar some stuff
> new code: foocombo foo foo bar bar foo bar some stuff foo

I don't know what version of Ubuntu you're running -- I'm not sure
which various versions of bash-3.2 are on Ubuntu 8, 9, or 10 -- but
using either your "old code" pattern after setting the `compat31'
option with shopt or the following

        wb='\b'
        ${wb}${i}${wb}

will work.  This is one of the reasons the compat31 option, and the
notion of a shell compatibility level, exists.  A shell variable will
usually do the job, too.

Item 33 in COMPAT and item E14 in the Bash FAQ cover the issue in
more detail.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]