help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] ${var//!([[:class:]])} removing characters of the [:clas


From: John Kearney
Subject: Re: [Help-bash] ${var//!([[:class:]])} removing characters of the [:class:]
Date: Sun, 27 Jan 2013 04:47:25 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2

Am 27.01.2013 01:24, schrieb Clark WANG:
On Sat, Jan 26, 2013 at 3:13 PM, Mart Frauenlob <address@hidden> wrote:
Hello,

am I wrong if I think that an extglob pattern of i.e. !([[:digit:]]) should match all non-digit characters? Why don't they match in the example below? Why are non-digit chars removed?

# cat extg
#!/bin/bash

shopt -s extglob

echo "address@hidden"

x=".:3:."

echo "Y${x}"
echo remove digits
echo "Y${x//@([[:digit:]])}"
echo remove non-digits
echo "Y${x//!([[:digit:]])}"

According to the bash manual, !(pattern-list) would match "anything except one of the given patterns". But the matching is string based rather than char based. So ${x//!([[:digit:]])} removed the string ".:3:." which does not match the pattern [[:digit:]].

More example:

$ shopt -s extglob
$ v=foobar
$ echo ${v//!(foo)/xxx}
xxx
$ echo ${v//!(bar)/xxx}
xxx
$

But I don't know how to explain this result:

$ echo ${v//!(foobar)/xxx}
xxxxxx
$
 
echo remove punct
echo "Y${x//+([[:punct:]])}"
echo remove non-punct
echo "Y${x//!([[:punct:]])}"

# /opt/bash42/bin/bash extg
4.2.42(1)-release
Y.:3:.
remove digits
Y.::.
remove non-digits
Y
remove punct
Y3
remove non-punct
Y


Best regards

Mart


if you just want to remove non-digits just use
${v//[![:digit:]]}
no need for extglob.

--
View John
          Kearney's profile on LinkedIn John Kearney

reply via email to

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