简体   繁体   English

不带数字的正则表达式C#文本

[英]Regular expression C# text without number

Please give me advice. 请给我建议。 How to write a regular expression, which allows only letter (without special character or number]? Valid inputs are for example: 如何编写仅允许字母(不包含特殊字符或数字)的正则表达式?有效输入例如:

  • oil, wheat, grain 油,小麦,谷物

not allowed for example: 不允许,例如:

  • 1oil, wheat5, grain33 1油,小麦5,谷物33

I try to write regular expression this way, but without success: 我尝试以这种方式编写正则表达式,但没有成功:

    public bool RegexControlProduct()
    {
        Regex regexObj = new Regex("[a-z]+");
        bool foundmatch = regexObj.IsMatch(subjectstring);
        return foundmatch;
    }

Thanks for your help. 谢谢你的帮助。

If you want to allow anything except numbers, you should do: 如果要允许除数字以外的任何内容,则应执行以下操作:

^[^\\d]+$

however, if you want to match the string in the format shown in your question, you can do: 但是,如果要以问题中显示的格式匹配字符串,则可以执行以下操作:

^[a-zA-Z,\\s]+$

Demo: http://regex101.com/r/iE2dJ9 演示: http//regex101.com/r/iE2dJ9

You are almost there, just use anchor with your regex. 您快到了,只需要在正则表达式中使用锚即可

Regex regexObj = new Regex("^[a-z]+$");
                            ^      ^

You may try to use [a-zA-Z]+ to handle both cases of letters. 您可以尝试使用[a-zA-Z]+处理两种字母的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM