Regex diacritics in C# not filtering on IsMatch -
i want return bool check string contains letters, numbers spaces , dashes. letters can foreign alphabets , need accept letters such é
, à
, î
, or ô
. line of code have:
if (regex.ismatch(this.userfirstname, @"^[a-za-z0-9À-ž_ -]") == false) { return false; }
however, returns true
if have #@!
in string; here on image switched ^
see if makes difference doesn't.
what need change expected result: if there characters such #
, @
, or !
in string return false
.
the main issue regex checks first character because of ^
anchor (start of string) , no quantifiers character class defined (i.e. 1 occurrence tested). thus, sylvain#@!
pass starts allowed character.
you need use \p{l}
shorthand class unicode letters both ^
, $
anchors , +
quantifier:
if (regex.ismatch(this.userfirstname, @"^[\p{l}\p{n}\p{zs}_-]+$") == false) { return false; }
see regexstorm demo
in regex, replaced 0-9
shorthand class \p{n}
(unicode class numeric characters), ,
\p{zs}
(unicode spaces). see more details @ msdn supported unicode general categories. if plan allow "regular" digits , regular space, keep 0-9
range , in regex:
@"^[\p{l}0-9 _-]+$"
Comments
Post a Comment