How to use RegEx in Dart?

后端 未结 2 965
说谎
说谎 2020-12-03 04:40

In a Flutter application, I need to check if a string matches a specific RegEx. However, the RegEx I copied from the JavaScript version of the app always returns fa

2条回答
  •  我在风中等你
    2020-12-03 05:08

    I think you tried to include options in the raw expression string while you already have it as parameters to RegEx ( /i for case insensitivity is declared as 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());
    

    Gives:

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

提交回复
热议问题