简体   繁体   English

Java 正则表达式到 Flutter 正则表达式

[英]Java Regex to Flutter Regex

I am trying to convert a Java regex to a Flutter(Dart) regex but I am having trouble.我正在尝试将 Java 正则表达式转换为 Flutter(Dart) 正则表达式,但我遇到了麻烦。

My Java Code and Regex example, witch returns Matching:我的 Java 代码和正则表达式示例,女巫返回匹配:

    public static void main(String[] args) {
    String EMAIL_VALIDATION_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
    String email = "testemail@gmail.com";
    if(!Pattern.matches(EMAIL_VALIDATION_REGEX, email)) {
        System.out.println("Not matching!");
    }else {
        System.out.println("Matching!");
    }
}

My Flutter Code example witch returns Not Matching:我的 Flutter 代码示例女巫返回不匹配:

    final RegExp _emailRegex = RegExp(
    r"^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$",
    caseSensitive: false,
    multiLine: false);

final String _email = "testemail@gmail.com";

if (!_emailRegex.hasMatch(_email)) {
  print("Not Matching!");
} else {
  print("Matching!");
}

I have basically copied the regex that i use in java and added r as a prefix.我基本上复制了我在 java 中使用的正则表达式,并添加了 r 作为前缀。 Can someone help me identify the issue with this code?有人可以帮我确定此代码的问题吗?

The r prefix creates a raw String. r前缀创建一个原始字符串。 As a result you don't need to double escape your \\.因此,您不需要双重转义\\. sequence.序列。 Use a single backslash: \.使用单个反斜杠: \.

See https://api.flutter.dev/flutter/dart-core/RegExp-class.htmlhttps://api.flutter.dev/flutter/dart-core/RegExp-class.html

Note the use of a raw string (a string prefixed with r) in the example above.请注意在上面的示例中使用了原始字符串(以 r 为前缀的字符串)。 Use a raw string to treat each character in a string as a literal character.使用原始字符串将字符串中的每个字符视为文字字符。

And https://www.learndartprogramming.com/fundamentals/strings-and-raw-strings-in-dart/https://www.learndartprogramming.com/fundamentals/strings-and-raw-strings-in-dart/

What is a raw strings in Dart: In Dart programming language, raw strings are a type of strings where special characters such as \ do not get special treatment.什么是 Dart 中的原始字符串:在 Dart 编程语言中,原始字符串是一种字符串类型,其中特殊字符(如 \)没有得到特殊处理。

Raw strings are useful when you want to define a String that has a lot of special characters.当你想定义一个有很多特殊字符的字符串时,原始字符串很有用。

Raw strings are also useful when you are defining complex regular expressions that otherwise are difficlut to define using the single and double quotes.当您定义复杂的正则表达式时,原始字符串也很有用,否则使用单引号和双引号很难定义。

The main advantage of using a raw String with regular expressions is that, unlike in Java, you can copy and paste your regex and test it easily in other tools, such as https://www.regexr.com , without worrying about additional escaping. The main advantage of using a raw String with regular expressions is that, unlike in Java, you can copy and paste your regex and test it easily in other tools, such as https://www.regexr.com , without worrying about additional escaping .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM