【问题标题】:How to use RegEx in Dart?如何在 Dart 中使用正则表达式?
【发布时间】:2018-09-20 07:13:06
【问题描述】:

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

感谢任何帮助!

正则表达式:/^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());

输出:

allMatches : ()
firstMatch : null
hasMatch : false
stringMatch : null

【问题讨论】:

    标签: regex dart flutter


    【解决方案1】:

    我认为您尝试在原始表达式字符串中包含选项,而您已经将其作为 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());
    

    给予:

    allMatches : (Instance of '_MatchImplementation')
    firstMatch : Instance of '_MatchImplementation'
    hasMatch : true
    stringMatch : WS://127.0.0.1:56789
    

    【讨论】:

    • 开头的/ 在 Dart AFAIK 中也不起作用
    • 谢谢! documentation 缺少这些基本但至关重要的信息,真是太可惜了。我只能希望 Dart 有一天会像 Go 一样有文档。
    • @NatoBoram,我对此表示赞同(我真的希望 Go 被选为 Flutter:)
    • 我可以看到RegExp 构造函数没有给出代码示例,您必须查看类文档才能看到。我们可能希望两者兼有,以便轻松捕获习惯于 JavaScript 正则表达式语法的用户。
    • @lrn,我想我也查看了类文档,但它不存在。如果我记错了,你能给个链接吗? Dart 文档感觉就像是由班级成员创建的自动文档,没有额外的解释和\或示例(有示例但很少,更像是尝试自己看看)。
    【解决方案2】:

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

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

    示例

    字母数字

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

    十六进制颜色

    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
    

    提取文本

    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
    

    还有一些例子here

    另见:

    【讨论】:

    • @LeoK,它确实有效,但在这种情况下它不是很有用。你得到的是一个包含两个项目的列表:字符串开头25F8 之前的所有内容(这是一个空字符串)以及它之后的所有内容。
    【解决方案3】:

    字符串模式 = r"[!-/:-@[-`{-~]";

    正则表达式 regExp = 正则表达式(模式);

    使用它它正在工作

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2022-11-25
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      相关资源
      最近更新 更多