Reputation: 2896
how not to allow special characters ? what I`ve tried is
final RegExp regex2 = RegExp(r"!^[`~!@#$%^&*()-_=]+$");
this seems not working and
final RegExp regex = RegExp(r"^[a-z0-9]+$");
I do not want this method too. This is because I have to allow not only English, but other languages as well.
Upvotes: 1
Views: 4488
Reputation: 584
RegExp(r"^[\p{L}\p{N}\s]+", unicode: true)
This line will :
Upvotes: 0
Reputation: 71828
If you want to allow letters and digits, but not just ASCII ones, you can use Unicode escapes:
var re = RegExp(r"^[\p{Letter}\p{Number}]+$", unicode: true);
or the shorthand:
var re = RegExp(r"^[\p{L}\p{N}]+$", unicode: true);
Trying to list all the non-letters is going to be extremely hard. Trying to list all the letters is going to be equally hard.
In practice, you probably want to allow combining marks as well, so maybe something like "é" can be matched both as a combined character and as e
followed by a combining accent:
var re = RegExp(r"^(?:\p{L}\p{Mn}*|\p{N})+$", unicode: true);
which also accepts the string "ide\u0301"
(containing the e
+accent combining mark for é
).
Whether that is sufficient depends on what your actual use case is.
If it's trying to recognize people's actual names, you probably need more characters. Some names contain dashes or apostrophes too, like
"Mary-Jane O'Reily".
(Also, there are some pretty funky \p{Letter}
s out there).
Upvotes: 6