Negate characters in Regular Expression [closed]
Asked Answered
C

4

74

How would I write a regular expression that matches the following criteria?

  • No numbers
  • No special characters
  • No spaces

in a string

Colossus answered 19/11, 2009 at 12:48 Comment(3)
So basically you only want letters?Doddering
can - you -clarify -your criteria? You want to match everything which has no numbers, no special characters (which ones?) and no spaces?Saltsman
I'm slightly confused by "special characters"...Resistant
B
122

The caret inside of a character class [^ ] is the negation operator common to most regular expression implementations (Perl, .NET, Ruby, Javascript, etc). So I'd do it like this:

[^\W\s\d]
  • ^ - Matches anything NOT in the character class
  • \W - matches non-word characters (a word character would be defined as a-z, A-Z, 0-9, and underscore).
  • \s - matches whitespace (space, tab, carriage return, line feed)
  • \d - matches 0-9

Or you can take another approach by simply including only what you want:

[A-Za-z]

The main difference here is that the first one will include underscores. That, and it demonstrates a way of writing the expression in the same terms that you're thinking. But if you reverse you're thinking to include characters instead of excluding them, then that can sometimes result in an easier to read regular expression.

It's not completely clear to me which special characters you don't want. But I wrote out both solutions just in case one works better for you than the other.

Band answered 19/11, 2009 at 14:16 Comment(2)
Note that the class \s not only matches white spaces, but all white space characters (line breaks and tabs as well). You (Steve) probably know this, but my comment might be beneficial to the OP.Tattered
[A-Za-z] excludes a lot more characters than what the OP asked. Although one can argue what is a “special character”, clearly accented Latin letters, Greek letters, Chinese characters, Tamil numbers, for instance, are not.Disabled
R
2

In Perl, it would be something like:

$string !~ /[\d \W]/

Of course, it depends on your definition of "special characters". \W matches all non-word characters. A word character is any alphanumeric character plus the space character.

Resistant answered 19/11, 2009 at 12:52 Comment(1)
Since you're only looking for a single character, you could simply do: $string !~ /\d|[ ]|\W/ which could be simplified as: $string !~ /[\d \W]/Tattered
C
-1

Try ^[^0-9\p{P} ]$

Cogswell answered 19/11, 2009 at 12:56 Comment(2)
Note that I interpreted "special characters" as puncuation. Not sure if that is what you want or not...Cogswell
I guess you'll want to remove a-zA-Z from it since you're negating it.Tattered
S
-7
var StringInputToClean = @"[:(M)?*a',\y<>&a#~%{}+.@\\ /27!;$+]";

var pattern = @"[^a-zA-Z0-9\s]";

string replacement = "";

var result = Regex.Replace(StringInputToClean, pattern, replacement);
Samos answered 16/6, 2014 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.