C# ^[A-Za-Z ][A-Za-z0-9 ]* 正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1653425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:42:43  来源:igfitidea点击:

^[A-Za-Z ][A-Za-z0-9 ]* regular expression?

c#regex

提问by Surya sasidhar

The regular expression ^[A-Za-Z ][A-Za-z0-9 ]*describe "first letter should be alphabet and remaining letter may be alpha numerical". But how do I also allow special characters? When I enter "C#" it is raising an error.

正则表达式^[A-Za-Z ][A-Za-z0-9 ]*描述“第一个字母应该是字母表,其余字母可能是字母数字”。但是我如何也允许特殊字符?当我输入“C#”时,它会引发错误。

How do I enter a special character and first letter should alphabet?

如何输入特殊字符和第一个字母应该是字母表?

回答by joemoe

Try this:

尝试这个:

^[A-Za-z ].*

^[A-Za-z].*

回答by Xinus

^[A-Za-z](\W|\w)*

(\W|\w)will ensure that every subsequent letter is word(\w) or non word(\W)

(\W|\w)将确保每个后续字母是 word( \w) 或非 word( \W)

instead of (\W|\w)*you can also use .*where .means absolutely anything just like (\w|\W)

而不是(\W|\w)*你也可以使用.*where.绝对意味着任何东西,就像(\w|\W)

回答by Asaph

This expression will force the first letter to be alphabetic and the remaining characters to be alphanumeric or any of the following special characters: @,#,%,&,*

此表达式将强制第一个字母为字母,其余字符为字母数字或以下任何特殊字符:@,#,%,&,*

^[A-Za-z][A-Za-z0-9@#%&*]*$

回答by mpen

How about

怎么样

^[A-Za-z]\S*

a letter followed by 0 or more non-space characters (will include all special symbols).

一个字母后跟 0 个或多个非空格字符(将包括所有特殊符号)。

回答by Peter Di Cecco

A lot of the answers given so far are pretty good, but you must clearly define what it is exactly that you want.

到目前为止给出的很多答案都非常好,但是您必须明确定义您想要的究竟是什么。

If you would like a alphabetical character followed by any number of non-white-space characters (note that it would also include numbers!) then you should use this:

如果您想要一个字母字符后跟任意数量的非空白字符(请注意,它还包括数字!),那么您应该使用:

^[A-Za-z]\S*$

If you would like to include only alpha-numeric characters and certain symbols, then use this:

如果您只想包含字母数字字符和某些符号,请使用以下命令:

^[A-Za-z][A-Za-z0-9!@#$%^&*]*$

Your original question looks like you are trying to include the space character as well, so you probably want something like this:

你原来的问题看起来你也试图包括空格字符,所以你可能想要这样的东西:

^[A-Za-z ][A-Za-z0-9!@#$%^&* ]*$

And that is my final answer!

这就是我的最终答案!

I suggest taking some time to learn more about regular expressions. They are the greatest thing since sliced bread!

我建议花一些时间来学习更多关于正则表达式的知识。它们是自切片面包以来最棒的东西!

Try this syntax referencepage (that site in general is very good).

试试这个语法参考页面(那个网站一般都很好)。

回答by Muhammad Mubashir

First must be Alphabet and then dot not allowed in target string. below is code.

首先必须是字母,然后在目标字符串中不允许使用点。下面是代码。

        string input = "A_aaA";

        // B
        // The regular expression we use to match
        Regex r1 = new Regex("^[A-Za-z][^.]*$"); //[\t
^[A-Za-z][A-Za-z0-9@#%&\*]*$
x0020] tab and spaces. // C // Match the input and write results Match match = r1.Match(input); if (match.Success) { Console.WriteLine("Valid: {0}", match.Value); } else { Console.WriteLine("Not Match"); } Console.ReadLine();

回答by sfdfsfsdf

This expression will check if the first letter to be alphabetic and the remaining characters to be alphanumeric or any of the following special characters: @,#,%,&,

此表达式将检查第一个字母是否为字母,其余字符是否为字母数字或以下任何特殊字符: @,#,%,&,

    If Regex.IsMatch(mystring, "^[A-Za-z ].*|\s") Then
        MessageBox.Show("please fill the box", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        GoTo labelname
    ElseIf (mystring = "") Then
        MessageBox.Show("please fill the box", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        GoTo labelname

    End If

回答by Sandhya Acharya

this expression is to get only numbers

这个表达式是只获取数字

##代码##