gpt4 book ai didi

regex - 为什么此正则表达式不返回任何匹配项?

转载 作者:行者123 更新时间:2023-12-04 16:27:16 24 4
gpt4 key购买 nike

我正在使用 dart regular expression并试图找到 match .

Dart 代码:http://try.dartlang.org/s/SY1B

  RegExp exp = const RegExp("/my/i");
String str = "Parse my string";
Iterable<Match> matches = exp.allMatches(str);
for (Match m in matches) {
String match = m.group(0);
print(match);
}

我正在尝试执行不区分大小写的搜索以查找字符串的所有匹配项。它说没有匹配项。我确定我在某个地方搞砸了,因为我是正则表达式的新手。如何修改代码以找到匹配项?

对于上下文,我计划修改代码以搜索我认为可以通过以下代码实现的任何字符串。

RegExp exp = const RegExp("/${searchTerm}/i");

最佳答案

/pattern/flags 语法适用于 JavaScript,但不适用于 Dart,因为 Dart 没有正则表达式文字。相反,文档显示了这一点:

const RegExp(String pattern, [bool multiLine, bool ignoreCase])

所以你的构造函数应该是这样的:

RegExp exp = const RegExp("my", ignoreCase: true);

关于regex - 为什么此正则表达式不返回任何匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10567187/

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