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

后端 未结 6 2197
南笙
南笙 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 06:11

    /// 
    /// Checks if string contains only letters a-z and A-Z and should not be more than 25 characters in length
    /// 
    /// String to be matched
    /// True if matches, false otherwise
    public static bool IsValidString(string value)
    {
        string pattern = @"^[a-zA-Z]{1,25}$";
        return Regex.IsMatch(value, pattern);
    }
    

提交回复
热议问题