Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong RegEx match in Dart

Tags:

regex

dart

The code itself: (you can see it on DartPad)

void main() {
   print(new RegExp("[0-9]|'|\"|\.").hasMatch('g')); // should return false
   print(new RegExp("[0-9]|'|\"|\.").hasMatch('0')); // return correctly true
}

Output:

true
true

With the same version on regex101 but with JS, the return value is correct.

Is there something I'm missing with my RegExp or should I report a bug?

like image 756
antonin.lebrard Avatar asked Feb 07 '23 21:02

antonin.lebrard


1 Answers

Either you use a raw string

print(new RegExp(r'''[0-9]|'|"|\.''').hasMatch('g'));

(''' is to avoid a conflict with " inside the string)

or escape \

print(new RegExp("[0-9]|'|\"|\\.").hasMatch('g'));

DartPad example

like image 105
Günter Zöchbauer Avatar answered Feb 09 '23 10:02

Günter Zöchbauer