This post presents a list of how zero length regular expression is handled in various tools and programming languages. All of them compile the zero length regular expression pattern and the regular expression matches all strings.

## GNU grep

    $ printf "foo\nbar\n" | grep ""
    foo
    bar

## BSD grep

    $ printf "foo\nbar\n" | grep ""
    foo
    bar

## Perl

    $ perl -e 'print(("foo" =~ //) . "\n")'
    1

## Python

    $ python
    Python 2.5.2 (r252:60911, Jan  4 2009, 21:59:32)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import re; re.compile('').search('foo')
    <_sre.SRE_Match object at 0x7fc6c5a2c510>

## Java

    $ cat RegexExperiment.java
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;

    public class RegexExperiment
    {
        public static void main(String[] args)
        {
            System.out.println(Pattern.compile("").matcher("foo").find());
        }
    }
    $ javac RegexExperiment.java && java RegexExperiment
    true

## MzScheme

    $ mzscheme
    Welcome to MzScheme v4.0.1 [3m], Copyright (c) 2004-2008 PLT Scheme Inc.
    > (regexp-match "" "foo")
    ("")

## CLISP

    $ clisp
      i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
      I I I I I I I      8     8   8           8     8     o  8    8
      I  \ `+' /  I      8         8           8     8        8    8
       \  `-+-'  /       8         8           8      ooooo   8oooo
        `-__|__-'        8         8           8           8  8
            |            8     o   8           8     o     8  8
      ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

    Welcome to GNU CLISP 2.44.1 (2008-02-23) <http://clisp.cons.org/>

    Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
    Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
    Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
    Copyright (c) Bruno Haible, Sam Steingold 1999-2000
    Copyright (c) Sam Steingold, Bruno Haible 2001-2008

    Type :h and hit Enter for context help.

    [1]> (regexp:match "" "foo")
    #S(REGEXP:MATCH :START 0 :END 0)

## C

    $ ls -l /usr/lib/libpcre.so*
    lrwxrwxrwx 1 root root     17 May  3 15:15 /usr/lib/libpcre.so -> libpcre.so.3.12.1
    lrwxrwxrwx 1 root root     17 Jan  6 14:57 /usr/lib/libpcre.so.3 -> libpcre.so.3.12.1
    -rw-r--r-- 1 root root 162816 Jul 14  2008 /usr/lib/libpcre.so.3.12.1
    susam@swift:~$ cat pcre.c
    #include <stdio.h>
    #include <string.h>
    #include <pcre.h>

    #include <stdio.h>
    #include <string.h>
    #include <pcre.h>

    int main(int argc, char **argv)
    {
        pcre *p;
        char *re = "";
        char *s  = "foo";
        const char *errmsg;
        int errpos;
        int ovector[10];
        int ret;

        p = pcre_compile(re, 0, &errmsg, &errpos, NULL);
        ret = pcre_exec(p, NULL, s, strlen(s), 0, 0,
                        ovector, sizeof ovector / sizeof *ovector);

        printf(ret < 0 ? "no match\n" : "match\n");
    }
    $ cc -lpcre pcre.c && ./a.out
    match