A list of how zero length regex is handled in various tools, programming languages, etc. All of them compile the zero length regex pattern and the regex matches all strings. It makes sense to me because if the regex pattern is a substring of a given string, it should match the given string. Zero length string ("") is a substring of every string.

grep

susam@swift:~\$ echo -ne "foo\nbar\n" | grep ""
foo
bar

Perl

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

Python

susam@swift:~\$ 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.
>>> re.compile('').search('foo')
<_sre.SRE_Match object at 0x7fc6c5a2c510>

Java

susam@swift:~\$ 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());
    }
}
susam@swift:~\$ javac RegexExperiment.java && java RegexExperiment
true

Scheme:

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

LISP:

susam@swift:~\$ 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)

libpcre3:

susam@swift:~\$ 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");
}

susam@swift:~\$ gcc -lpcre pcre.c && ./a.out
match

No comments

Post a comment

RSS