Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dart Regex for matching number or certain characters at the end of String

I’m trying to check for either numbers, ‘e’, ‘π’ or ‘ ‘ at the end of a String.

This is what I’m using: \s|[0-9]|e|π$. This is the code:

final regex = RegExp(r'\s|[0-9]|e|π$');

if (regex.hasMatch(_expression)) {  //_expression is the string
    //body
}

This always returns true. What’s the error here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try with the following regular expression:

void main() {
  final regex = RegExp(r'(\s|[0-9]|e|π)$');

  print(regex.hasMatch('959**')); // false
  print(regex.hasMatch('959')); // true
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading