tags:

views:

192

answers:

9

Hi

I am working in asp.net. I am using Regular Expression Validator

Could you please help me in creating a regular expression for not allowing special characters other than comma. Comma has to be allowed.

I checked in regexlib, however I could not find a match. I treid with "^(a-z|A-Z|0-9)*[^#$%^&*()']*$" . When I add other characters as invalid, it does not work.

Also could you please suggest me a place where I can find a good resource of regular expressions? regexlib seems to be big; but any other place which lists very limited but most used examples?

Also, can I create expressions using C# code? Any articles for that?

Anyway, my immediate requirement is the special character problem

Thanks Lijo

A: 

For a single character that is not a comma, [^,] should work perfectly fine.

Jimmie Lin
+3  A: 

[\d\w\s,]*

Just a guess

Martin Milan
You probably shouldn't submit just guesses as answers. I'm not saying your answer is wrong, I'm just saying it's not the best practice. Plus people will vote you down if your guess is wrong and you'll lose points.
Jordan S
@Martin Milan,"\w " means alpha-numeric characters. So again \d is not necessary. \w will take care of numbers also.
Shekhar
Fair enough Jordon - didn't have time to test it, but I'll bear your comment in mind in future...
Martin Milan
Cheers Shekhar - that's why I like this site...
Martin Milan
+1  A: 
Nick Craver
I believe the OP wants *nothing* but the allowed chars in the string: `^[\w\s,]*$`
Jørn Schou-Rode
A: 

Hi Lijo,

You can try [\w\s,] regular expression. This regex will match only alpha-numeric characters and comma. If any other character appears within text, then this wont match.

For your second question regarding regular expression resource, you can goto

http://www.regular-expressions.info/

This website has lot of tutorials on regex, plus it has lot of usefult information.

Also, can I create expressions using C# code? Any articles for that?

By this, do you mean to say you want to know which class and methods for regular expression execution? Or you want tool that will create regular expression for you?

Shekhar
One more question... What if I should not allow space also? I tried with "[\w,]*". It works fine if I am entering space aftere a character. If I enter space first, it does not work. Please help
Lijo
To allow space, you can use [\w\s,]
Shekhar
+2  A: 
[\w\s,]+

works fine, as you can see bellow.
alt text

RegExr is a great place to test your regular expressions with real time results, it also comes with a very complete list of common expressions.

[] character class \w Matches any word character (alphanumeric & underscore). \s Matches any whitespace character (spaces, tabs, line breaks). , include comma + is greedy match; which will match the previous 1 or more times.

Mohammad
@Mohammad, +1, excellent Regex tool!
MagicAndi
`[...]` is called a character class, not a capturing set.
Alan Moore
ty, updated the post!
Mohammad
A: 

You can create expressions with C#, something like this usually does the trick:

        Regex regex = new Regex(@"^[a-z | 0-9 | /,]*$", RegexOptions.IgnoreCase);
        System.Console.Write("Enter Text");
        String s = System.Console.ReadLine();
        Match match = regex.Match(s);
        if (match.Success == true)
        {
            System.Console.WriteLine("True");
        }

        else
        {
            System.Console.WriteLine("False");
        }
        System.Console.ReadLine();

You need to import the System.Text.RegularExpressions; The regular expression above, accepts only numbers, letters (both upper and lower case) and the comma.

For a small introduction to Regular Expressions, I think that the book for MCTS 70-536 can be of a big help, I am pretty sure that you can either download it from somewhere or obtain a copy.

I am assuming that you never messed around with regular expressions in C#, hence I provided the code above.

Hope this helps.

npinti
The pipe and the spaces don't belong in there; they'll match actual pipes and spaces. Also, I'm not sure why you put that `/` in there, but it doesn't belong either; `[a-z0-9,]` is all you need. And unless the OP specifically states that empty strings are valid, you should use `+` instead of `*`.
Alan Moore
I assumed that an empty string would also be, sort of valid.Apologies for the mistakes I made in the regular expression, how ever, when I tried it out it did work so I do not think that it is completely wrong.I will keep in mind the + thing, thanks for the correction.
npinti
A: 

Thank you, all..

[\w\s,]* works

Let me go through regular-expressions.info and come back if I need further support.

Let me try the C# code approach and come back if I need further support.

[This forum is awesome. Quality replies so qucik..]

Thanks again

Lijo
Hi,I meant integers, alphabets and comma
Lijo
A: 

One more question... What if I should not allow space also? I tried with "[\w,]*". It works fine if I am entering space aftere a character. If I enter space first, it does not work. Please help

Lijo
You can use [\w\s,]* where \w stands for alphanumberic character, \s stands for whitespace character which includes space, tab,etc. and * represents zero or more occurances of \w or \s or ,
Shekhar
A: 

(…) is denoting a grouping and not a character set that’s denoted with […]. So try this:

^[a-zA-Z0-9,]*$

This will only allow alphanumeric characters and the comma.

Gumbo
I tried. It allows space as a first character in the text box. Any other thoughts?
Lijo
@Lijo: That can’t be. How do you test it?
Gumbo
Hi Gumbo, I am using it in asp.net as follows <asp:RegularExpressionValidator ID="regularexpressionvalidator4" runat="server" ControlToValidate="txtAreaNo" Display="dynamic" ErrorMessage="rrr" ValidationExpression="^[a-zA-Z0-9,]*$" ValidationGroup="ttt"><img src="../../images/icon_ttt.gif" alt="field" /> </asp:RegularExpressionValidator>
Lijo