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.

7
  • What would be a valid value? Commented Aug 8, 2016 at 21:16
  • Try regexr.com Dart only supports what JS RegExp supports to be xxxToJS transpilable. Commented Aug 8, 2016 at 21:18
  • Use numbered capture groups: ^(.*m)(\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}) ([^\s]+) (.*) and access them with number indices. Commented Aug 8, 2016 at 21:21
  • 1
    Thank You @GünterZöchbauer for editing the original answer. The valid value will be suffix=[0m[31m , time=22:25:57,366 , level=error, message=.... Commented Aug 8, 2016 at 21:22
  • @WiktorStribiżew How to use numbered capture groups with dart Regex ? Commented Aug 8, 2016 at 21:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thank You. it's ok. I've created a dartpad sample to share the solution dartpad.dartlang.org/0bd2244aa9e93c300282d269f2eda0be
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.
This is no longer the case. Named groups are now supported. api.dartlang.org/stable/2.3.1/dart-core/RegExpMatch/…
@ScottHyndman For some reason, named groups are not working in DartPad. Also, according to Strings and regular expressions documentation section, Dart regex is based on 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".
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.
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

3 Comments

No it's not working in dartpad. It throws an error in 2.4.1.
Hi Gaurav, if you try the dartpad link provided does it work? It works for me.
Hi Gav, it worked here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.