【问题标题】:Why is My Regex Code not working on DART?为什么我的正则表达式代码在 DART 上不起作用?
【发布时间】:2022-01-07 21:46:46
【问题描述】:

我从网站正文中获取数据。然后我编写了一个正则表达式并应用于 DART,但它不起作用。问题是什么?

这是正则表达式代码:

</td><td align="left">(.*?)</td><td class="dataGridActive

这是我的部分内容:

</tr><tr onmouseover="mover(this);" onmouseout="mout(this);" style="background-color:White;">
            <td align="left">233</td><td align="left">ÖMER EFE CIKIT</td><td class="dataGridActive" align="center">

还有飞镖代码:

void CheckRE(String text) {
    final RegExp pattern = RegExp(
      r'</td><td align="left">(.*?)</td><td class="dataGridActive"',
      multiLine: true,
      caseSensitive: true,
    ); // 800 is the size of each chun
    pattern
        .allMatches(text)
        .forEach((RegExpMatch match) => print(match.group(1)));
  }

【问题讨论】:

  • 预期结果是什么?
  • @julemand101 'ÖMER EFE CIKIT'

标签: regex dart


【解决方案1】:

我认为您想要的是以下内容。

我已更改您的输出,因此它打印捕获组 1 的内容而不是捕获组 0。捕获组 0 包含匹配的整个字符串,而 1 及以上包含正则表达式中每个已定义捕获组的内容。

const input = '''
</tr><tr onmouseover="mover(this);" onmouseout="mout(this);" style="background-color:White;">
            <td align="left">233</td><td align="left">ÖMER EFE CIKIT</td><td class="dataGridActive" align="center">
''';

void main() => checkRE(input); // ÖMER EFE CIKIT

void checkRE(String text) {
  final RegExp pattern = RegExp(
    r'</td><td align="left">(.*?)</td><td class="dataGridActive"',
    multiLine: true,
    caseSensitive: true,
  ); // 800 is the size of each chun
  pattern.allMatches(text).forEach((RegExpMatch match) => print(match[1]));
}

还根据@MikeM 的建议将(.*) 更改为(.*?)

【讨论】:

  • 我已经更新了问题部分。但还是一样。不返回任何东西
  • @Cellesss 你能像我一样制作一个独立的程序来重现你的问题吗?如果我复制您的代码和输入,它在 DartVM 中运行时对我来说很好。
  • 我添加了另一个评论。
猜你喜欢
  • 2018-04-01
  • 2012-11-17
  • 2022-01-20
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 2014-06-01
相关资源
最近更新 更多