How can I get a regex to check that a string only contains alpha characters [a-z] or [A-Z]?

后端 未结 6 2191
南笙
南笙 2020-12-05 05:24

I\'m trying to create a regex to verify that a given string only has alpha characters a-z or A-Z. The string can be up to 25 letters long. (I\'m not sure if regex can check

6条回答
  •  佛祖请我去吃肉
    2020-12-05 05:59

    Do I understand correctly that it can only contain either uppercase or lowercase letters?

    new Regex("^([a-z]{1,25}|[A-Z]{1,25})$")
    

    A regular expression seems to be the right thing to use for this case.

    By the way, the caret ("^") at the first place inside a character class means "not", so your "[^a-z]|[^A-Z]" would mean "not any lowercase letter, or not any uppercase letter" (disregarding that a-z are not all letters).

提交回复
热议问题