3

I'm building a log parser in dart. How can I use dart regex library to retrieve matching values.

Using this link to check my regex, everything is ok wih the following :

  • Regex : (?<suffix>^.*m)(?<time>\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}) (?<level>[^\s]+) (?<message>.*)
  • Input : [0m[31m22:25:57,366 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.deployment.unit."bad.war".

Entering these values will give you : suffix, time , level and message

I'm not able to use my regex with the dart library.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
bwnyasse
  • 591
  • 1
  • 10
  • 15

2 Answers2

8

When running Dart in a browser, it uses the browser's regex engine, and if it supports named capturing groups they will be supported. If not, they won't. When running your code in Flutter, or standalone Dart, namedGroup works as you'd expect.

Use numbered capturing groups and access them by their indices:

RegExp regExp = new RegExp(r"^(.*m)(\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}) ([^\s]+) (.*)");

See this regex demo

In Dart, try something like this:

RegExp regExp = new RegExp(r"^(.*m)(\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}) ([^\s]+) (.*)");
String s = "[0m[31m22:25:57,366 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.deployment.unit.\"bad.war\"";
Iterable<Match> matches = regExp.allMatches(s);
for (Match match in matches) {
  print("${match.group(1)}\n");
  print("${match.group(2)}\n");
  print("${match.group(3)}\n");
  print("${match.group(4)}\n");
}

See this DartPad demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank You. it's ok. I've created a dartpad sample to share the solution https://dartpad.dartlang.org/0bd2244aa9e93c300282d269f2eda0be – bwnyasse Aug 08 '16 at 21:43
  • Oh, I have not seen the comment before I posted my example :) Glad you found a way quicker. I just had some problems with getting the demo work on the Dart pad. – Wiktor Stribiżew Aug 08 '16 at 21:54
  • This is no longer the case. Named groups are now supported. https://api.dartlang.org/stable/2.3.1/dart-core/RegExpMatch/namedGroup.html – Scott Hyndman Jun 11 '19 at 05:25
  • @ScottHyndman For some reason, named groups are not working in DartPad. Also, according to [*Strings and regular expressions*](https://api.dartlang.org/stable/2.3.1/dart-core/dart-core-library.html#strings-and-regular-expressions) documentation section, Dart regex is based on [ecma-international.org/ecma-262/5.1/#sec-15.10](http://ecma-international.org/ecma-262/5.1/#sec-15.10) that does not support named groups (their support is introduced in ECMAScript 2018). Something here does not "click". – Wiktor Stribiżew Jun 11 '19 at 09:30
  • Ah. When Dart is running in the browser it's likely using the browser's regex engine. When running in Flutter, or standalone Dart, namedGroups work as you'd expect. – Scott Hyndman Jun 21 '19 at 17:11
0

For future readers, as Scott Hyndman explains in a comment, this is now supported with RegExpMatch. https://api.dartlang.org/stable/2.4.0/dart-core/RegExp-class.html

This does appear to be working in DartPad now (with dart 2.4.0 at least). https://dartpad.dartlang.org/de5fafc572ebb786c9b4791229c65a9b

Gav
  • 55
  • 8