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

时间:2022-03-15 16:47:21

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?

如何输入一个特殊的字符和首字母?

8 个解决方案

#1


15  

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 reference page (that site in general is very good).

请尝试这个语法参考页面(该站点通常非常好)。

#2


3  

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@#%&*]*$

#3


2  

Try this:

试试这个:

^[A-Za-z ].*

^[A-Za-z]。*

#4


1  

How about

如何

^[A-Za-z]\S*

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

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

#5


0  

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\0x0020] 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();

#6


0  

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: @,#,%,&,

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

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

#7


0  

this expression is to get only numbers

这个表达式只得到数字

    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

#8


-5  

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

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

(\W|\ W)会确保以后的每一个信都是word(\W)或non - word(\W)

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

你也可以用在哪里,而不是(\W|\ W)*。意味着任何事情都像(\w|\ w)

#1


15  

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 reference page (that site in general is very good).

请尝试这个语法参考页面(该站点通常非常好)。

#2


3  

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@#%&*]*$

#3


2  

Try this:

试试这个:

^[A-Za-z ].*

^[A-Za-z]。*

#4


1  

How about

如何

^[A-Za-z]\S*

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

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

#5


0  

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\0x0020] 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();

#6


0  

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: @,#,%,&,

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

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

#7


0  

this expression is to get only numbers

这个表达式只得到数字

    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

#8


-5  

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

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

(\W|\ W)会确保以后的每一个信都是word(\W)或non - word(\W)

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

你也可以用在哪里,而不是(\W|\ W)*。意味着任何事情都像(\w|\ w)