Flutter cannot parse regex

Viewed 1203

Flutter cannot parse this working regex, and doesn't return any error or info.

(?<=id=)[^&]+

However, when I add it into my Flutter app:

print("before");
new RegExp(r'(?<=id=)[^&]+');
print("after");

It doesn't do anything, doesn't return any error. The print("after"); never gets executed. It doesn't completly freeze the app, because it's in async.

2 Answers

Dart compiled for the Web supports lokbehinds, but the current version of native Dart (including Flutter) does not support lookbehinds (source).

In your case, you want to match a string after a specific string. All you need is to declare a capturing group in your pattern and then access that submatch:

RegExp regExp = new RegExp(r"id=([^&]+)");
String s = "http://example.com?id=some.thing.com&other=parameter; http://example.com?id=some.thing.com";
Iterable<Match> matches = regExp.allMatches(s);
for (Match match in matches) {
    print(match.group(1));
}

Output:

some.thing.com
some.thing.com

Here, id=([^&]+) matches id= and then the ([^&]+) capturing group #1 matches and captures into Group 1 any one or more chars other than &. Note you may make it safer if you add [?&] before id to only match id and not thisid query param: [?&]id=([^&]+).

Related