Solved

URGENT: Easy Regex Question

Posted on 2004-04-07
3
288 Views
Last Modified: 2010-04-15
I'm missing something here,  I only want to allow alphanumeric characters and the period.  i have the following code

Regex regex = new Regex("[^[a-z][A-Z][0-9].]");
if(regex.IsMatch(txtName.Text) == true)

Do I need a carrot before each of those sub groups?  

(the code is not working properly).
0
Comment
Question by:jjacksn
  • 2
3 Comments
 
LVL 2

Expert Comment

by:eric_duncan
ID: 10780716
Try this instead, it should do what you want:

Regex regex = new Regex("^[a-zA-Z0-9.]+$");


0
 
LVL 2

Accepted Solution

by:
eric_duncan earned 500 total points
ID: 10780781
To explain further, tou need to group all of the characters you are looking for into a single group rather than creating three consecutive groups because the regex parser will process this as looking for 3 consecutive characters rather than a single character that is a member of all three groups.

In plain english, your old regex does this:

First, match any character not in "[a-z",
followed by any character in "A-Z",
followed by any character in "0-9",
followed by any character,
followed by ].

The new one supplied above does this:

First, start at the beginning of the string,
then match any character in "a-zA-Z0-9." and continue doing this until the end of the string is reached.

Does this help?

Eric
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10780886
Regex regex = new Regex("[a-zA-Z0-9.]+");
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question