GNU bug report logs - #21159
Fails To Match Empty String

Previous Next

Package: grep;

Reported by: Squirrely <squirrely <at> gmx.com>

Date: Wed, 29 Jul 2015 22:51:02 UTC

Severity: normal

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#21159: closed (Fails To Match Empty String)
Date: Thu, 30 Jul 2015 00:54:01 +0000
[Message part 1 (text/plain, inline)]
Your message dated Wed, 29 Jul 2015 17:53:32 -0700
with message-id <55B9758C.4010606 <at> cs.ucla.edu>
and subject line Re: bug#21159: Fails To Match Empty String
has caused the debbugs.gnu.org bug report #21159,
regarding Fails To Match Empty String
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
21159: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=21159
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Squirrely <squirrely <at> gmx.com>
To: bug-grep <at> gnu.org
Subject: Fails To Match Empty String
Date: Wed, 29 Jul 2015 23:38:47 +0100
Hi

I'm a bit of a regular expression noob, so I'm not sure if this is a bug
or if I'm just missing something about how grep works.

Here's a demo of the issue I have encountered:

> bash$ rm empty
> bash$ touch empty
> bash$ # I am expecting a match, so grep should return 0.
> bash$ grep '^$' empty
> bash$ echo $?
> 1
> bash$ # Hmmm... weird.
> bash$ # Same example but using STDIN instead...
> bash$ echo -n ""| grep '^$'
> bash$ echo $?
> 1
> bash$ # Same result.  How does the python re module treat this?
> bash$ python3
> >>> import re
> >>> m = re.search("^$", "")
> >>> type(m)
> <class '_sre.SRE_Match'>
> >>> # A match was found.  Python returns 'None' if it's not a match,
> >>> # like this...
> >>> m = re.search("fo?", "bar")
> >>> type(m)
> <class 'NoneType'>

I know that the Python re module and grep use a different regex syntax,
but I'm pretty sure "^$" has the same meaning for both.

I discounted the idea that grep only checks lines that end with a
newline character because of this:

> bash$ echo -en "foo\nfoo\nfoo"|grep foo
> foo
> foo
> foo

As you can see, the third foo is checked and matched despite not being
terminated with a newline character (observe the echo "-n" switch).

So... why does ^$ match the empty string with python but not with grep?

-Squirrely


[Message part 3 (message/rfc822, inline)]
From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Squirrely <squirrely <at> gmx.com>, 21159-done <at> debbugs.gnu.org
Subject: Re: bug#21159: Fails To Match Empty String
Date: Wed, 29 Jul 2015 17:53:32 -0700
Squirrely wrote:
> bash$ rm empty
>>bash$ touch empty
>>bash$ # I am expecting a match, so grep should return 0.
>>bash$ grep '^$' empty
>>bash$ echo $?

Grep looks for lines that contain matches.  An empty file has no lines, so it 
cannot possibly contain any matches for any regular expression.



This bug report was last modified 9 years and 360 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.