473,883 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex to test Alpha characters

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,
Nov 15 '05 #1
4 11424
"Tim Conner" <ti*******@hotm ail.com> wrote in
news:07******** *************** *****@phx.gbl:
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.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
"Tim Conner" <ti*******@hotm ail.com> wrote in
news:07******** *************** *****@phx.gbl:
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(V alue));
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #3
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..#2 54]);
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,

Nov 15 '05 #4
"Tim Conner" <an*******@disc ussions.microso ft.com> wrote in
news:0c******** *************** *****@phx.gbl:
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..#2 54]);
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5735
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning is to improve, but not to prove.
3
2461
by: Steven Berkovitz | last post by:
I was just wondering if there is a reason (maybe a RFC requirement?) that the built in email address regular expression for the RegularExpressionValidator in ASP.NET doesn't support email aliases ending in a dash. For example, the following address will not validate: test-address-@hotmail.com Thanks, -Steven
0
992
by: sd | last post by:
Are any of the special range metacharacters allowed in RegEx operations(System.Text.RegularExpressions) . If so how could you please specify an example. Thx! sd Only the digits 0 to 9 Any alphanumeric character 0 to 9 OR A to Z or a to z.
8
1690
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me a way to see that the data is in set by parenthesising it. This is all generated as I said using pexpect - Here is how I use it.. child = pexpect.spawn( _buildCadenceExe(), timeout=timeout) child.sendline("somefunction()")
4
2100
by: skavan | last post by:
Use Case: We have music files that describe, in their filename, attributes of the music. We do not know a general pattern that applies to all filenames -- but we do know that filenames that are clustered together (by for example directory) will, most likely, have the same filename pattern. Here is an example: 10,000 Maniacs - MTV Unplugged - 01 - These are Days.mp3
4
333
by: Bill Mill | last post by:
Hello all, I've got a test script: ==== start python code ===== tests2 = def test_re(regex): r = re.compile(regex, re.MULTILINE)
10
3912
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program is using Python's standard re (regular expressions) module to parse the input file. As Python's routines to read from the file and regular expressions are most likely implemented via native libraries I would expect that the C code, which reads...
3
2711
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def RN(name, regex): """protect using () and give an optional name to a regex""" if name:
15
50303
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
9953
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10868
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7980
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.