Thread overview
Is std.regex.match completely broken?
Feb 28, 2011
Jacob Carlborg
Mar 01, 2011
Dmitry Olshansky
Mar 01, 2011
Jacob Carlborg
February 28, 2011
The following code will result in an AssertError or RangeError when run.

import std.regex;
import std.stdio;

void main ()
{
    auto m = "abc".match(`a(\w)b`);

    writeln(m.hit); // AssertError in regex.d:1795
    writeln(m.captures); // RangeError in regex.d:1719
}

Should I report this as a bug?

-- 
/Jacob Carlborg
March 01, 2011
On 28.02.2011 22:37, Jacob Carlborg wrote:
> The following code will result in an AssertError or RangeError when run.
>
> import std.regex;
> import std.stdio;
>
> void main ()
> {
>     auto m = "abc".match(`a(\w)b`);
>
>     writeln(m.hit); // AssertError in regex.d:1795
>     writeln(m.captures); // RangeError in regex.d:1719
> }
>
> Should I report this as a bug?
>
Well, there won't be a match.
If you meant "abc".match(`a(\w)c`) then it works for me.
At the bottom of it all, I also was sort of surprised to get an Assert and not an Exception, but it's the way it works with ranges in Phobos. So you should check m.empty before use.
P.S. I'm in the process of patching in lookahead regexes, I think I can get them fairly soon. As for lookbehind, well, that's would be somewhat harder it seems.

-- 
Dmitry Olshansky

March 01, 2011
On 2011-03-01 14:03, Dmitry Olshansky wrote:
> On 28.02.2011 22:37, Jacob Carlborg wrote:
>> The following code will result in an AssertError or RangeError when run.
>>
>> import std.regex;
>> import std.stdio;
>>
>> void main ()
>> {
>> auto m = "abc".match(`a(\w)b`);
>>
>> writeln(m.hit); // AssertError in regex.d:1795
>> writeln(m.captures); // RangeError in regex.d:1719
>> }
>>
>> Should I report this as a bug?
>>
> Well, there won't be a match.
> If you meant "abc".match(`a(\w)c`) then it works for me.
> At the bottom of it all, I also was sort of surprised to get an Assert
> and not an Exception, but it's the way it works with ranges in Phobos.
> So you should check m.empty before use.

That seems quite strange, to design an API like that. Why doesn't "hit" just returns an empty string and "captures" an empty range.

> P.S. I'm in the process of patching in lookahead regexes, I think I can
> get them fairly soon. As for lookbehind, well, that's would be somewhat
> harder it seems.

Sounds good.

-- 
/Jacob Carlborg