如何在Dart中使用RegEx?

Nat*_*ram 16 regex dart flutter

在Flutter应用程序中,我需要检查字符串是否与特定的RegEx匹配.但是,我从应用程序的JavaScript版本复制的RegEx 在Flutter应用程序中始终返回false.我在regexr上验证了RegEx是有效的,并且这个RegEx已经在JavaScript应用程序中使用了,所以它应该是正确的.

任何帮助表示赞赏!

RegEx: /^WS{1,2}:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:56789/i

测试代码:

RegExp regExp = new RegExp(
  r"/^WS{1,2}:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:56789/i",
  caseSensitive: false,
  multiLine: false,
);
print("allMatches : "+regExp.allMatches("WS://127.0.0.1:56789").toString());
print("firstMatch : "+regExp.firstMatch("WS://127.0.0.1:56789").toString());
print("hasMatch : "+regExp.hasMatch("WS://127.0.0.1:56789").toString());
print("stringMatch : "+regExp.stringMatch("WS://127.0.0.1:56789").toString());
Run Code Online (Sandbox Code Playgroud)

输出:

allMatches : ()
firstMatch : null
hasMatch : false
stringMatch : null
Run Code Online (Sandbox Code Playgroud)

Cet*_*soz 19

我认为您尝试在原始表达式字符串中包含选项,而您已将它作为RegEx的参数(/ i表示不区分大小写被声明为caseSensitive:false).

// Removed /i at the end
// Removed / in front - Thanks to Günter for warning
RegExp regExp = new RegExp(
  r"^WS{1,2}:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:56789",
  caseSensitive: false,
  multiLine: false,
);
print("allMatches : "+regExp.allMatches("WS://127.0.0.1:56789").toString());
print("firstMatch : "+regExp.firstMatch("WS://127.0.0.1:56789").toString());
print("hasMatch : "+regExp.hasMatch("WS://127.0.0.1:56789").toString());
print("stringMatch : "+regExp.stringMatch("WS://127.0.0.1:56789").toString());
Run Code Online (Sandbox Code Playgroud)

得到:

allMatches : (Instance of '_MatchImplementation')
firstMatch : Instance of '_MatchImplementation'
hasMatch : true
stringMatch : WS://127.0.0.1:56789
Run Code Online (Sandbox Code Playgroud)

  • 开头的`/`在Dart AFAIK中也不起作用 (2认同)
  • 谢谢![文档](https://api.dartlang.org/stable/1.24.3/dart-core/RegExp-class.html) 缺乏这些基本但关键的信息,这真是令人遗憾。我只能希望 Dart 有朝一日能像 Go 一样被记录在案。 (2认同)
  • @NatoBoram,我是第二个(我真的希望 Go 被选为 Flutter :) (2认同)

Sur*_*gch 7

对于将来的观众来说,这是一个更普遍的答案。

Dart中的Regex的工作方式与其他语言非常相似。您可以使用RegExp该类来定义匹配的模式。然后使用hasMatch()来测试字符串上的模式。

例子

字母数字

final alphanumeric = RegExp(r'^[a-zA-Z0-9]+$');
alphanumeric.hasMatch('abc123');  // true
alphanumeric.hasMatch('abc123%'); // false
Run Code Online (Sandbox Code Playgroud)

十六进制颜色

RegExp hexColor = RegExp(r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$');
hexColor.hasMatch('#3b5');     // true
hexColor.hasMatch('#FF7723');  // true
hexColor.hasMatch('#000000z'); // false
Run Code Online (Sandbox Code Playgroud)

提取文字

final myString = '25F8..25FF    ; Common # Sm   [8] UPPER LEFT TRIANGLE';

// find a variable length hex value at the beginning of the line
final regexp = RegExp(r'^[0-9a-fA-F]+'); 

// find the first match though you could also do `allMatches`
final match = regexp.firstMatch(myString);

// group(0) is the full matched text
// if your regex had groups (using parentheses) then you could get the 
// text from them by using group(1), group(2), etc.
final matchedText = match.group(0);  // 25F8
Run Code Online (Sandbox Code Playgroud)

有一些更多的例子在这里