Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Google Dart have something like regex.exec()?

Tags:

dart

I read the documentation (https://api.dartlang.org/stable/1.21.1/dart-core/RegExp-class.html) but could not find I was looking for. Either I didnt understand it or I overlooked something.

I am trying to replicate the following in google dart:

var regex = /foo_(\d+)/g,
    str = "text foo_123 more text foo_456 foo_789 end text",
    match = null;

while (match = regex.exec(str)) {
    console.log(match); // matched capture groups
    console.log(match.index); // index of where match starts in string
    console.log(regex.lastIndex); // index of where match ends in string
}

I also created a jsfiddle: https://jsfiddle.net/h3z88udz/

Does dart have something like regex exec()?

like image 845
Asperger Avatar asked Jun 03 '26 21:06

Asperger


1 Answers

RegExp.allMatches looks like it does what you want.

var regex = new RegExp(r"foo_(\d+)");
var str = "text foo_123 more text foo_456 foo_789 end text";

void main() {
  for (var match in regex.allMatches(str)) {
    print(match);
    print(match.start);
    print(match.end);
  }
}

https://dartpad.dartlang.org/dd1c136fa49ada4f2ad4ffc0659aab51

like image 75
Harry Terkelsen Avatar answered Jun 06 '26 20:06

Harry Terkelsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!