I having the following regexp for validating the function name:
([a-zA-Z]\w+)[^\w]
This regular expression is capable of avoiding special characters except underscore but if we put special characters in the beginning then t开发者_如何学JAVAhis expression can't validate.
So can you make it work little better?
thanks in adv
You need to match from the beginning to the end of the whole function name:
^([a-zA-Z]\w+)[^\w]$
Following the C# language specification:
http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx
The following regex will handle special cases of '_' and '@', plus validate non ASCII letters i.e. validates unicode identifiers.
[Test]
public void ValidateIdentifiers()
{
Regex regex = new Regex(
@"^@?_*[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Nl}\p{Mn}\p{Mc}\p{Cf}\p{Pc}\p{Lm}]*$");
Assert.That(regex.IsMatch("Bling"), Is.True);
Assert.That(regex.IsMatch("@_Bling"), Is.True);
Assert.That(regex.IsMatch("_Bling"), Is.True);
Assert.That(regex.IsMatch("__Bling"), Is.True);
Assert.That(regex.IsMatch("_Bling_Bling"), Is.True);
Assert.That(regex.IsMatch("السياحى"), Is.True);
Assert.That(regex.IsMatch("_@Bling"), Is.False);
}
Right now the expression is saying: one letter followed by one or more letter/numbers/underscores, followed by a non-word character. So by design, the fact that it won't validate if you start with a special character is correct.
If you want to allow for the _ as the first letter, change to:
(\w+)[^\w]
精彩评论