ZetCode

Dart 正则表达式

最后修改日期:2024 年 1 月 28 日

在本文中,我们将介绍如何在 Dart 中使用正则表达式。

正则表达式用于文本搜索和更高级的文本操作。我们在 grep 和 sed 等工具、vi 和 Emacs 等文本编辑器以及编程语言中都能找到它们。

RegExp 用于定义正则表达式。Dart 正则表达式的语法和语义与 JavaScript 正则表达式相同。

Dart 正则表达式 hasMatch

hasMatch 函数检查正则表达式是否在指定的字符串中匹配。

main.dart
void main() {
  var words = <String>["Seven", "even", "Maven", "Amen", "eleven"];

  var rx = RegExp(r".even");

  for (var word in words) {
    if (rx.hasMatch(word)) {
      print("${word} does match");
    } else {
      print("${word} does not match");
    }
  }
}

在此示例中,我们在列表中有五个单词。 我们检查哪些单词与 .even 正则表达式匹配。

var rx = RegExp(r".even");

我们使用 RegExp 定义一个正则表达式。点号(.)元字符在文本中代表任何单个字符。

for (var word in words) {
  if (rx.hasMatch(word)) {
      print("${word} does match");
  } else {
      print("${word} does not match");
  }
}

我们遍历列表的元素,并使用 hasMatch 检查元素是否匹配正则表达式。

$ dart main.dart
Seven does match
even does not match
Maven does not match
Amen does not match
eleven does match

Dart 正则表达式 匹配项

交替运算符 | 创建具有多个选择的正则表达式。

main.dart
void main() {
  var words = [
    "Jane",
    "Thomas",
    "Robert",
    "Lucy",
    "Beky",
    "John",
    "Peter",
    "Andy"
  ];

  var rx = RegExp("Jane|Beky|Robert");

  words.forEach((word) {
    if (rx.hasMatch(word)) {
      print("the ${word} matches");
    }
  });
}

列表中有八个名称。

var rx = RegExp("Jane|Beky|Robert");

这个正则表达式查找 "Jane"、"Beky" 或 "Robert" 字符串。

$ dart main.dart
the Jane matches
the Robert matches
the Beky matches

Dart 正则表达式 子模式

子模式是模式中的模式。 子模式使用 () 字符创建。

main.dart
void main() {
  var words = [
    "book",
    "bookshelf",
    "bookworm",
    "bookcase",
    "bookish",
    "bookkeeper",
    "booklet",
    "bookmark"
  ];

  var rx = RegExp(r"^book(worm|mark|keeper)?$");

  for (var word in words) {
    if (rx.hasMatch(word)) {
      print("${word} does match");
    } else {
      print("${word} does not match");
    }
  }
}

我们查找所有匹配子模式的单词。

var rx = RegExp(r"^book(worm|mark|keeper)?$");

正则表达式使用一个子模式。 它匹配 bookworm、bookmark、bookkeeper 和 book 单词。

$ dart main.dart
book does match
bookshelf does not match
bookworm does match
bookcase does not match
bookish does not match
bookkeeper does match
booklet does not match
bookmark does match

Dart RegExp allMatches

allMatches 函数查找给定正则表达式的所有匹配项。

main.dart
void main() {
  var text = 'The Sun was shining; I went for a walk.';

  var rx = RegExp(r"\w+");

  var found = rx.allMatches(text);
  print("There are ${found.length} words");

  found.forEach((match) {
    print("${match.group(0)} ${match.start}:${match.end}");
  });
}

在示例中,我们查找文本中的所有单词。

var rx = RegExp(r"\w+");

该正则表达式匹配所有单词;单词定义为一个或多个字符。

print("There are ${found.length} words");

我们打印单词的数量。

found.forEach((match) {
  print("${match.group(0)} ${match.start}:${match.end}");
});

我们遍历所有找到的匹配项,并打印它们及其索引。

$ dart mains.dart
There are 9 words
The 0:3
Sun 4:7
was 8:11
shining 12:19
I 21:22
went 23:27
for 28:31
a 32:33
walk 34:38

Dart 正则表达式 捕获组

圆括号用于创建捕获组。这使我们能够将量词应用于整个组,或将交替限制为正则表达式的一部分。

main.dart
void main() {
  var sites = ["webcode.me", "zetcode.com", "freebsd.org", "netbsd.org"];

  var rx = RegExp(r"(\w+)\.(\w+)");

  for (var site in sites) {
    var matches = rx.allMatches(site);
    matches.forEach((match) {
      print(match.group(0));
      print(match.group(1));
      print(match.group(2));
    });
    print('----------------------');
  }
}

在示例中,我们使用组将域名分成两部分。

var rx = RegExp(r"(\w+)\.(\w+)");

我们使用括号定义两个组。点号被转义,因为它在这里用于字面意义。

var matches = rx.allMatches(site);

我们使用 allMatches 查找所有匹配项。

print(match.group(0));
print(match.group(1));
print(match.group(2));

可以通过 group 函数访问组。默认的整个匹配项可以通过 match.group(0) 获得。

$ dart main.dart 
webcode.me
webcode
me
----------------------
zetcode.com
zetcode
com
----------------------
freebsd.org
freebsd
org
----------------------
netbsd.org
netbsd
org
----------------------

来源

Dart RegExp - 语言参考

在本文中,我们介绍了 Dart 正则表达式。

作者

我叫 Jan Bodnar,是一位充满激情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。迄今为止,我已撰写了 1400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出 所有 Dart 教程