gpt4 book ai didi

regex - Flutter 无法解析正则表达式

转载 作者:IT老高 更新时间:2023-10-28 12:40:59 24 4
gpt4 key购买 nike

Flutter 无法解析 this working regex ,并且不返回任何错误或信息。

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

但是,当我将它添加到我的 Flutter 应用程序中时:

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

它不做任何事情,不返回任何错误。 print("after"); 永远不会被执行。它不会完全卡住应用程序,因为它是异步的。

最佳答案

为 Web 编译的 Dart 支持 lokbehinds,但当前版本的原生 Dart(包括 Flutter)不支持lookbehinds (source)。

在您的情况下,您希望在特定字符串之后匹配一个字符串。您只需在模式中声明一个捕获组,然后访问该子匹配:

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));
}

输出:

some.thing.com
some.thing.com

这里 id=([^&]+) 匹配 id= 然后是 ([^&]+) 捕获组#1 匹配并捕获除 & 之外的任何一个或多个字符到第 1 组。请注意,如果您在 id 之前添加 [?&] 以仅匹配 id 而不是 thisid 可能会更安全查询参数:[?&]id=([^&]+).

关于regex - Flutter 无法解析正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53986424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com