当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Dart RegExp.allMatches用法及代码示例


dart:core 库中RegExp.allMatches 方法的用法介绍如下。

用法:

Iterable<RegExpMatch> allMatches(
   String input,    
   [int start = 0]   
)
      override

将此模式与字符串重复匹配。

如果提供了start,则匹配将从该索引开始。

返回的可迭代对象懒惰地在 string 中找到模式的非重叠匹配项。如果用户只请求第一个匹配项,则此函数不应计算所有可能的匹配项。

通过重复查找字符串中模式的第一个匹配项来找到匹配项,最初从 start 开始,然后从上一个匹配的结尾开始(但始终至少比上一个匹配的 start 晚一个位置,以防模式匹配空子字符串)。

RegExp exp = RegExp(r'(\w+)');
var str = 'Dash is a bird';
Iterable<Match> matches = exp.allMatches(str, 8);
for (final Match m in matches) {
  String match = m[0]!;
  print(match);
}

该示例的输出是:

a
bird

相关用法


注:本文由纯净天空筛选整理自dart.dev大神的英文原创作品 allMatches method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。