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

Alex Folland lexlexlex at gmail.com
Sun Jan 30 23:28:25 PST 2011


On 2011-01-31 0:50, Alex Folland wrote:
> On 2011-01-30 21:47, Vladimir Panteleev wrote:
>> On Mon, 31 Jan 2011 03:57:44 +0200, Alex Folland <lexlexlex at gmail.com>
>> wrote:
>>
>>> 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?
>>
>> "*" in regular expressions means 0 or more instances of the previous
>> entity:
>> http://www.regular-expressions.info/repeat.html
>> It doesn't make sense at the start of an expression. ".*" is the regexp
>> that matches anything[1].
>>
>> std.regex probably can't handle invalid regexps very well. Note that
>> std.regex is a new module that intends to replace the older std.regexp,
>> but still has some problems.
>
> Okay, so that particular regex is invalid. Yeah, it still shouldn't
> crash. You're right. How should I prevent my program from crashing
> without fixing std.regex (code I definitely don't trust myself to
> touch)? Would the Scope statement be useful? I still can't figure out
> exactly what it does. I tried using scope(exit)writeln("Bad regex.");
> just before my foreach loop, but it still crashes. I then tried changing
> "exit" to "failure", but that didn't help either; same behavior. Am I
> using scope wrong?

Yeah, you nitwit.  You didn't realize that scope(failure) doesn't 
prevent the program from exiting and throwing an exception.  It merely 
runs the code inside itself before throwing an exception and exiting. 
However, one can use scope(failure){writeln("Bad regex");break;} to 
prevent an actual crash and just write "Bad regex"!  I expected that it 
would break automatically out of scope.  I was wrong.  Anyway, this is 
beautiful.  I love D.  Haha.


More information about the Digitalmars-d-learn mailing list