tags:

views:

198

answers:

5

I am trying to make a regular expression for a string that has at least 1 non alphanumeric symbol in it

The code I am trying to use is

Regex symbolPattern = new Regex("?[!@#$%^&*()_-+=[{]};:<>|./?.]");

I'm trying to match only one of !@#$%^&*()_-+=[{]};:<>|./?. but it doesn't seem to be working.

+7  A: 

If you want to match non-alphanumeric symbols then just use \W|_.

Regex pattern = new Regex(@"\W|_");

This will match anything except 0-9 and a-z. Information on the \W character class and others available here.

JaredPar
Not to challenge a grand master, but don't you mean `\W` ? `\w` (lower case w) matches all word characters.
Alan
This is almost completely wrong.
just somebody
Yet it got 3 upvotes.
Alan
@Alan, yep meant exactly \W. Somehow missed the caps. Thanks!
JaredPar
@just somebody: I rolled back your rev. @JaredPar had made the appropriate fixes. Sometimes people make typos etc. It was a simple mistake. He fixed it. I'll give him the benefit of the doubt that he knows the difference between match all word chars and all non word chars :D
Alan
@Alan, @just somebody, yep it was just as simple typo. I just missed the SHIFT key while adding the other escaping
JaredPar
@Alan, JaredPar: i'm ok with the rollback, and my gripe was meant at the upvoters, not Jared. exactly as you wrote: everybody makes mistakes (note, i didn't even downvote the answer).
just somebody
I thought once you hit 100k rep you ascend to godhood and whatnot. :)
ChaosPandion
@Chaos, I assure you that is not the case ;)
JaredPar
@Jared: The `\W` class excludes underscores as well as alphanumerics, so might not be suitable for the OP's needs. (The set of characters shown in the question includes underscore.)
LukeH
@LukeH, good catch. Updated my answer
JaredPar
@Chaos: Jared is just saying that cuz first rule about godhood, you don't talk about godhood.
Alan
A: 

Can you check for the opposite condition?

Match match = Regex.Match(@"^([a-zA-Z0-9]+)$");
if (!match.Success) {
    // it's alphanumeric
} else {
    // it has one of those characters in it.
}
jskaggz
+3  A: 

You could also avoid regular expressions if you want:

return s.Any(c => !char.IsLetterOrDigit(c))
ChaosPandion
Now for extra credit: which is more performant?
Alan
Hehe.. Sometimes you just HAVE to use a regex for everything :) Even though I love regular expressions, it's often best to avoid them.
simendsjo
@Alan - I'll run a test later (unless someone else chimes in) but I am confident that this is a bit faster.
ChaosPandion
@Chaos: also try with compiled regex.
Alan
`ToCharArray` will make a copy of the string's characters and is unnecessary. For better performance just query the string itself: `return s.Any(c => !char.IsLetterOrDigit(c));`
LukeH
A: 

I didn't get your entire question, but this regex will match those strings that contains at least one non alphanumeric character. That includes whitespace (couldn't see that in your list though)

[^\w]+
simendsjo
+2  A: 

Your regex just needs little tweaking. The hyphen is used to form ranges like A-Z, so if you want to match a literal hyphen, you either have to escape it with a backslash or move it to the end of the list. You also need to escape the square brackets because they're the delimiters for character class. Then get rid of that question mark at the beginning and you're in business.

Regex symbolPattern = new Regex(@"[!@#$%^&*()_+=\[{\]};:<>|./?,-]");

If you only want to match ASCII punctuation characters, this is probably the simplest way. \W matches whitespace and control characters in addition to punctuation, and it matches them from the entire Unicode range, not just ASCII.

You seem to be missing a few characters, though: the backslash, apostrophe and quotation mark. Adding those gives you:

@"[!@#$%^&*()_+=\[{\]};:<>|./?,\\'""-]"

Finally, it's a good idea to always use C#'s verbatim string literals (@"...") for regexes; it saves you a lot of hassle with backslashes. Quotation marks are escaped by doubling them.

Alan Moore