C# Regex - Accept spaces in a string -
i have application needs verifications fields. 1 of them last name can composed of 2 words. in regex, have accept these spaces tried lot of things did'nt find solution.
here regex :
@"^[a-za-zàéèêçñ\s][a-za-zàéèêçñ-\s]+$"
the \s
spaces not work , got error message :
parsing "^[a-za-zàéèêçñ\s][a-za-zàéèêçñ-\s]+$" - cannot include class \s in character range.
any idea guys?
-
denotes character range, use a-z
describe any character between , z. regex uses ~n-\s
engine tries interpret any character between ~n , \s -- , notices, \s
doesn't make whole lot of sense there, because \s
abbreviation whitespace character.
that's error comes from.
to rid of this, should put -
@ end of character class, if want include -
literal character:
@"^[a-za-zàéèêçñ\s][a-za-zàéèêçñ\s-]+$"
this way, engine knows \s-
not character range, 2 characters \s
, -
seperately.
the other way escape -
character:
@"^[a-za-zàéèêçñ\s][a-za-zàéèêç\-\s]+$"
so engine interprets ~n\-\s
not character range, any of characters ~n
, -
or \s
. personally, though try avoid escaping possible, because imho clutters , needlessly stretches expression in length.
Comments
Post a Comment