Regex to test Alpha characters

T

Tim Conner

I wrote the following function to test a string for
characters from a to z and the "_" character :

public static bool IsAlpha( char Value )
{
Regex regExp = new Regex("^[a-zA-Z_]*$");
if (regExp.IsMatch( Value.ToString() ))
{
return true;
}
else return false;
}

However, I've realized, that my function needs to accept
accented characters as well.
I mean, characters like áéíóúâä, etc.
How do I write my regex to allow them ?


Regards,
 
C

Chris R. Timmons

I wrote the following function to test a string for
characters from a to z and the "_" character :

public static bool IsAlpha( char Value )
{
Regex regExp = new Regex("^[a-zA-Z_]*$");
if (regExp.IsMatch( Value.ToString() ))
{
return true;
}
else return false;
}

However, I've realized, that my function needs to accept
accented characters as well.
I mean, characters like áéíóúâä, etc.
How do I write my regex to allow them ?

Tim,

Use the Unicode character class \p{L} (anything considered a letter):

^[\p{L}_]*$

Hope this helps.

Chris.
 
C

Chris R. Timmons

I wrote the following function to test a string for
characters from a to z and the "_" character :

public static bool IsAlpha( char Value )
{
Regex regExp = new Regex("^[a-zA-Z_]*$");
if (regExp.IsMatch( Value.ToString() ))
{
return true;
}
else return false;
}

However, I've realized, that my function needs to accept
accented characters as well.
I mean, characters like áéíóúâä, etc.
How do I write my regex to allow them ?

Tim,

I shouldn't have posted so fast...

I just realized the System.Char class has an IsLetter method that
will do what you want. You can use it like this to check for
letters or underscores:

public static bool IsAlpha( char Value )
{
return ((Value == '_') || char.IsLetter(Value));
}


Hope this helps.

Chris.
 
T

Tim Conner

Chris,

Thanks much for your advise.
I am in my first days with C# and I'm really passing hard
days without the lovely Delphi's sets.

This was my function in Delphi :

function IsAlpha(Value: Char): Boolean;
begin
Result:=(Value in ['A'..'Z','a'..'z','_',#128..#254]);
end;

Can we say that there is no direct equivalent to this ?
Is this regex all I have to perform tasks like that ?

I've just visit your site and saw that you're Delphi
programmer too !! :)
That's why I am asking this more directly.


Thanks in advance,
 
C

Chris R. Timmons

Chris,

Thanks much for your advise.
I am in my first days with C# and I'm really passing hard
days without the lovely Delphi's sets.

This was my function in Delphi :

function IsAlpha(Value: Char): Boolean;
begin
Result:=(Value in ['A'..'Z','a'..'z','_',#128..#254]);
end;

Can we say that there is no direct equivalent to this ?
Is this regex all I have to perform tasks like that ?

Tim,

True, C# has no "set" datatype like Delphi does. However, here's
a like to some free code that implements a C# Set class that provides
all of Delphi's set capabilities and more:

http://www.codeproject.com/csharp/Sets.asp

For very simple membership tests, Arrays can also be used to emulate
some functionality of sets. Fill the array with data, then use the
IndexOf method to test for membership.

Hope this helps.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top