Matches exactly regular expression not work in dart

Issue

Java script regular expression

/\d(.*)\s[0-9A-Z:]{17}/i

Test Case

console.log("11  salus_biura                      68:d7:9a:da:7d:6a   WPA2PSK/AES            29       11b/g/n NONE   In  NO    ".match(/\d(.*)\s[0-9A-Z:]{17}/i)[1]);

Dart code

void main() {
   String wifiInfo = "11  salus_biura                      68:d7:9a:da:7d:6a   WPA2PSK/AES            29       11b/g/n NONE   In  NO    ";
   Match? matches =   RegExp(r'\\d(.*)\\s[0-9A-Z:]{17}', caseSensitive: true).firstMatch(wifiInfo);
   print(matches?.group(1)); // null 
} 

issue {17} Matches exactly not work

output in dart 1 salus_biura

Solution

In your Dart code,

  • Use caseSensitive: true as you have an i flag (that enables case insensitive matching) in your JavaScript regex
  • Use single backslashes in a raw string literal. "\\d" = r"\d".

The fix code can look like

void main() {
  String wifiInfo = "11  salus_biura                      68:d7:9a:da:7d:6a   WPA2PSK/AES            29       11b/g/n NONE   In  NO    ";
  Match? matches =   RegExp(r'\d(.*)\s[0-9A-Z:]{17}', caseSensitive: false).firstMatch(wifiInfo);
  print(matches?.group(1)); // => 1  salus_biura   
}

Answered By – Wiktor Stribiżew

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *