mogoh
mogoh

Reputation: 1071

Why does Dart (not) match this RegEx?

I have got this small bit of code:

void main() {
    RegExp re = new RegExp(r".*(a+)+\d");
    print("a0aaaaaaaaaaaaa".contains(re));
    print("a0aaaaaaaaaaaaaa".contains(re));
}

Output:

true
false

Why? I would expect both to be true.

Edit: I filed a bug report: https://code.google.com/p/dart/issues/detail?id=19193

Upvotes: 1

Views: 2092

Answers (2)

lrn
lrn

Reputation: 71623

The behavior is definitely a bug.

It might be a common and deliberate bug in JS RegExp implementations. If a RegExp runs for "too long", some implementations give up and claim not to match.

I'm guessing (because I haven't checked yet) that it is the VM's RegExp implementation that bails out because the RegExp is taking too long to complete. The RegExp in question looks exactly like one that would cause catastrophic backtracking (taking up to exponential time in the length of the input). That would explain why increasing the length causes it to suddenly give up and say that it doesn't match, even though clearly it should.

Upvotes: 2

hwnd
hwnd

Reputation: 70722

Can't reproduce your result using Try Dart online compiler, they both do successfully return true, but it does return true and false using another Dart compiler.

Try using the following instead.

RegExp re = new RegExp(r"(a+)\d");

Upvotes: 2

Related Questions