Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use dynamic (variable) string as regex pattern in dart

i am trying to pass variable in regex language DART

  `betweenLenth(val, field, [min = 4, max = 20]) {
     final RegExp nameExp = new RegExp(r'^\w{" + min + "," + max + "}$');
     if (!nameExp.hasMatch(val))
     return field + " must be between $min - $max characters ";
   }`

thanks

like image 701
user5084949 Avatar asked May 16 '18 12:05

user5084949


1 Answers

You can't use string-interpolation with raw strings.

With interpolation

final RegExp nameExp = new RegExp('^\\w{"$min","$max"}\$');
final RegExp nameExp = new RegExp('^\\w{$min,$max}\$');

with concatenation

final RegExp nameExp = new RegExp(r'^\w{"' + min + '","' + max + r'"}$');
final RegExp nameExp = new RegExp(r'^\w{' + min + ',' + max + r'}$');
like image 164
Günter Zöchbauer Avatar answered Sep 28 '22 16:09

Günter Zöchbauer