logo头像
Snippet 博客主题

Dart 入门基础 正则表达式

本文于323天之前发表,文中内容可能已经过时。

Dart 入门基础 正则表达式

部分属性

1
2
3
4
5
6
7
8
9
10
11
RegExp exp = new RegExp(r"(\w+)");
// 返回正则表达式的哈希码
print(exp.hashCode);
// 正则表达式是否区分大小写
print(exp.isCaseSensitive);
// 正则表达式是否匹配多行
print(exp.isMultiLine);
// 返回源正则表达式字符串
print(exp.pattern);
// 返回对象运行时的类型
print(exp.runtimeType);

常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
RegExp exp = new RegExp(r"(\w+)");
// 返回正则表达式匹配项的可迭代对象
print(exp.allMatches("abc def ghi"));
// 搜索并返回第一个匹配项,没有则返回null
print(exp.firstMatch(""));
// 正则表达式是否找到匹配项
print(exp.hasMatch("as"));
// 从第几个字符开始匹配正则表达式
print(exp.matchAsPrefix("ab cd", 3));
// 返回正则表达式的第一个匹配字符串
print(exp.stringMatch("abc de"));
// 返回正则表达式的字符串表示
print(exp.toString());

例子

验证邮政编码的正则,返回是否匹配的布尔值

1
2
RegExp postalcode = new RegExp(r'(\d{6})');
print(postalcode.hasMatch("518000"));

验证手机号码的正则,以Iterable< Match >返回所有匹配项

1
2
3
4
5
6
RegExp mobile = new RegExp(r"(0|86|17951)?(13[0-9]|15[0-35-9]|17[0678]|18[0-9]|14[57])[0-9]{8}");
Iterable<Match> mobiles = mobile.allMatches("13812345678 12345678901 17012345678");
for (Match m in mobiles) {
String match = m.group(0);
print(match);
}

验证网址URL的正则,如果匹配成功以Match返回匹配项,否则返回null

1
2
RegExp url = new RegExp(r"^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+");
print(url.firstMatch("http://www.google.com"));

验证身份证号码的正则,返回第一个匹配的字符串

1
2
RegExp identity = new RegExp(r"\d{17}[\d|x]|\d{15}");
print(identity.stringMatch("My id number is 35082419931023527x"));
支付宝打赏 微信打赏

打赏