Regular Expression to reject special characters other than commas

10,137

Solution 1

[\w\s,]+

works fine, as you can see bellow.

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.

Solution 2

[\d\w\s,]*

Just a guess

Solution 3

To answer on any articles, I got started here, find it to be an excellent resource:
http://www.regular-expressions.info/

For your current problem, try something like this:

[\w\s,]*

Here's a breakdown:

  • Match a single character present in the list below «[\w\s,]*»
    • Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    • A word character (letters, digits, etc.) «\w»
    • A whitespace character (spaces, tabs, line breaks, etc.) «\s»
    • The character “,” «,»
Share:
10,137
Lijo
Author by

Lijo

Updated on June 13, 2022

Comments

  • Lijo
    Lijo almost 2 years

    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?