bug-coreutils
[Top][All Lists]
Advanced

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

Re: bug in expr


From: Eric Blake
Subject: Re: bug in expr
Date: Sat, 15 Nov 2008 12:50:10 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.17) Gecko/20080914 Thunderbird/2.0.0.17 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Dmitry Dobrushin on 11/15/2008 11:41 AM:

Hello Dmitry,

Thanks for the report; however, this is not a bug.

> command:
> expr match "ut96_01_200" "\([0-9][0-9]_[0-9][0-9]\)"
> 
> did not return,

It returned, it just gave the (correct) answer of an empty string.  expr
implicitly anchors its regexp at the start of the string, and 'u' does not
match [0-9].

> while another command:
> 
> expr match "ut96_01_200" ".*\([0-9][0-9]_[0-9][0-9]\)"
> 
> return : "01_20"

Regular expressions are greedy - if more than one candidate matches the
pattern (in your case, both ut96_01 and ut96_01_20), then the longest
match is chosen.

> 
> What should I do to get the desired "96_01" result?

Depends on what pattern you will be matching against (in other words, I
don't know what format of strings you are trying to match - are they
always two letters followed by your desired pattern?).  But for this
particular example, you could use:

expr match "ut96_01_200" "[^0-9]*\([0-9][0-9]_[0-9][0-9]\)"

Or, to be more portable to POSIX (since 'match' is an extension), you
should use the colon operator:

expr "ut96_01_200" : "[^0-9]*\([0-9][0-9]_[0-9][0-9]\)"

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkkfJ/EACgkQ84KuGfSFAYDkvgCgop78aVS4L4dcqnLX/n0DdFW8
or8An1bCkEG0478O6DgqARPLPsouPLDL
=n4vp
-----END PGP SIGNATURE-----




reply via email to

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