www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is std.regex.match completely broken?

reply Jacob Carlborg <doob me.com> writes:
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
Feb 28 2011
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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?

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
Mar 01 2011
parent Jacob Carlborg <doob me.com> writes:
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?

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
Mar 01 2011