Why does "*" cause my tiny regextester program to crash?

Alex Folland lexlexlex at gmail.com
Sun Jan 30 17:57:44 PST 2011


I wrote this little program to test for regular expression matches.  I 
compiled it with in Windows with DMD 2.051 through Visual Studio 2010 
with Visual D.  It crashes if regexbuf is just the single character, 
"*".  Why?  Shouldn't it match the entire string?

Visual Studio's debug output is this:

First-chance exception at 0x76fde124 in regextester.exe: 0xE0440001: 
0xe0440001.
The program '[5492] regextester.exe: Native' has exited with code 1 (0x1).

Also, why does it match an unlimited number of times on "$" instead of 
just once?  Is this a Phobos-specific issue, or are regular expressions 
supposed to do that?  I mean, it doesn't match an unlimited times on 
"h", for example.

Mind you, I'm very new to both regular expressions and D.  I'm also not 
an experienced programmer of anything else.  I've spent years dabbling 
in the surface various programming languages without learning anything 
meaty.  I've learned syntax mostly.

My debug build is here: http://lex.clansfx.co.uk/projects/regextester.exe

Here's the source code:

import std.stdio, std.regex;

void main()
{
   char[] regexbuf;
   char[] teststring;
   while(1)
   {
     write("test string: ");
     std.stdio.readln(teststring); teststring.length=teststring.length-1;
     while(teststring.length>0)
     {
       uint i=0;
       write("regex input: ");
       std.stdio.readln(regexbuf); regexbuf.length=regexbuf.length-1;
       if(regexbuf.length>0)
       foreach(m; match(teststring, regex(regexbuf)))
       {
         i++;
         writefln("Match number %s: %s[%s]%s",i,m.pre,m.hit,m.post);
         if(i >= 50) { writefln("There have been %s matches.  I'm 
breaking for safety.",i); break; }
       }
     }
   }
}


More information about the Digitalmars-d-learn mailing list