Re: Regular Expression ^^$$?

From: Eli the Bearded (*_at_eli.users.panix.com)
Date: 04/21/04


Date: Wed, 21 Apr 2004 20:36:22 +0000 (UTC)

In comp.os.linux.misc, Doug O'Leary <dkoleary@olearycomputers.com> wrote:
> The question apparently didn't give any information on the
> regex engine (perl/sed/awk, etc). All's it did was give the
> pattern and ask what does it match? It was apparently a
> multiple guess question only one answer of which could my
> cohort remember.

Well, that sucks becuase different regexp engines will handle it
differently.

> The pattern is: ^^$$.

In procmail '^^' matches at the very beginning(*) of a possibly multiline
string, like \A in perl, and $ matches a newline. So, '^^$$' would match
two blank lines at the start of the search space.
(*) And very end, hence '^^^^' to match an empty string. Procmail is weird.

$ cat > /tmp/test.rc
STRING='

Both lines above are empty.'

:0
* STRING ?? ^^$$
{ LOG='STRING matched.
'
}
^D
$ echo | procmail -m /tmp/test.rc
STRING matched.
$ procmail -v
procmail v3.22 2001/09/10
[...]

In perl, $$ will expand to the pid of the current process, and the double
caret will be treated the same as a single caret. So, '^^$$' would match
the pid of the perl process found at the start of a line.

$ perl -we 'print "Pid: $$\n";$_ = $$;if(/^^$$/){print "Matched\n"}'
Pid: 29968
Matched
$ perl -v

This is perl, v5.6.1 built for i686-linux
[...]

In grep (gnu grep at least), the ^ and $ will only be special in places
where they can possibly match the start or end of a line. So, '^^$$'
would match a line that contained exactly '^$'.

$ echo '^$' | grep -c '^^$$'
1
$ grep -V
grep (GNU grep) 2.5.1
[...]

Solaris grep (and ed on Solaris) behave like grep above.

That's several programs with three very different results.

Elijah
------
learned perl after his ed scripts got too hairy



Relevant Pages

  • Re: Working with files
    ... string appears only in one of the files in the directory. ... Well, until you said that, the correct answer was not to use Perl at ... but instead to just use grep: ... 1: perldoc -f opendir ...
    (perl.beginners)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)