Solved

URGENT: Easy Regex Question

Posted on 2004-04-07
3
285 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
Comment Utility
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
Comment Utility
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
Comment Utility
Regex regex = new Regex("[a-zA-Z0-9.]+");
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now