Paolo
Paolo

Reputation: 1637

regex: plus sign vs asterisk

The asterisk or star tells the engine to attempt to match the preceding token zero or more times. The plus tells the engine to attempt to match the preceding token once or more.

Based on the definition, I was wondering why the plus sign returns more matches than the asterisk sign.

 echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]+"

returns

ABC DDD

 echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]*"

returns ABC

Upvotes: 0

Views: 1994

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263237

As far as I can tell, it doesn't. With GNU grep versions 2.5.3, 2.6.3, 2.10, and 2.12, I get:

$ echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]+"
ABC
DDD
$ echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]*"
ABC
DDD

Please double-check your second example. If you can confirm that you get only one line of output, it might be a bug in your grep. If you're using GNU grep, what's the output of grep --version? If not, what OS are you using, and (if you know) what grep implementation?

UPDATE :

I just built and installed GNU grep 2.5.1 (the version you're using) from source, and I confirm your output. It appears to be a bug in that version of grep, apparently corrected between 2.5.1a and 2.5.3. GNU grep 2.5.1 is about 12 years old; can you install a newer version? Looking through the ChangeLog for 2.5.3, I suspect this may have been the fix:

2005-08-24  Charles Levert  <[email protected]>

    * src/grep.c (print_line_middle): In case of an empty match,
      make minimal progress and continue instead of aborting process
      of the remainder of the line, in case there's still an upcoming
      non-empty match.
    * tests/foad1.sh: Add two tests for this.
    * doc/grep.texi, doc/grep.1: Document this behavior, since
      --only-matching and --color are GNU extensions which are
      otherwise unspecified by POSIX or other standards.

Even if you don't have full access on the machine you're using, you should still be able to download the source tarball from ftp://ftp.gnu.org/gnu/grep/ and install it under your home directory (assuming your system has a working compiler and associated tools).

Upvotes: 5

Related Questions