John Shedletsky
John Shedletsky

Reputation: 7168

.Net Regex that Matches Strings With Any non-ASCII char in it

Looking for some black magic that will match any string with "weird" characters in it. Standard ASCII characters are fine. Everything else isn't.

This is for sanitizing various web forms.

Upvotes: 5

Views: 4523

Answers (2)

Jon Hanna
Jon Hanna

Reputation: 113242

[^\p{IsBasicLatin}] for what is asked for, [^\x00-\x7F] for concision over self-documentation, or \p{C} for clearing out formatters and controls without hurting other non-ASCIIs (and with greater concision yet).

Upvotes: 2

NullUserException
NullUserException

Reputation: 85458

This gets anything out of the ASCII range

[^\x00-\x7F]

There are still some "weird" characters like x00 (NULL), but they are valid ASCII.
For reference, see the ASCII table

Upvotes: 7

Related Questions