tags:

views:

175

answers:

7

Hi i need a regex that matches all instances of any character that is not a-z (space and things like apostrophes need to be selected). Sorry for the noob factor. thanks!

//novice

+1  A: 

The character class [^a-zA-Z] will match any character that isn't (upper or lowercase) a-z.

I'm sure you can figure out the rest.

Anon.
thanks! this one works for the preg_replace in php, and Asaphs version worked for the duplicate one i was using in Jquery.
mroggle
A: 

Here is regex that will literally match any char that is not a-z. The /g flag indicates a global match which will cover all instances of the match.

/[^a-z]+/g

If you need uppercase letters too, you can either pass the /i flag which indicates case insensitivity:

/[^a-z]+/gi

or include the uppercase chars in character class:

/[^a-zA-Z]+/g
Asaph
This will also match no character at all.
Gumbo
@Gumbo: Good point. I fixed it by changing `*` to `+`.
Asaph
many thanks! this worked perfectly when used in javascript
mroggle
+1  A: 

\W will match any non-alphanumeric (a-z, 0-9, and underscore) character.

Asher Dunn
A: 

The following regular expression matches any letter other than [a-z]:

/[^a-z]+/
Alan Haggai Alavi
A: 

OK.

  1. /[^a-z]+/ will match anything other than lowercase letters.
  2. /[^A-Za-z]+/ will match anything non-alpha.
  3. /\W+/ on most systems will match non-'word' characters. Word characters include A-Z, a-z, 0-9, and '_' (underscore). Note that that is an uppercase W.
Peter Rowell
+2  A: 

With a somewhat sophisticated regex engine (grep will do just fine) this will be quite general:

/[^[:lower:]]+/

(Note the ^!)

The difference between [:lower:] and [a-z] is that the former should be I18N friendly and match e.g. ü, â etc.

For case insensitive matching use [:alpha:], to also include digits use [:alnum:]. [:alnum:] differs from \W in that it doesn't include _ (underscore).

Note that character classes written in this style may be combined as usual (like a-z etc.), e.g. [^[:lower:][:digit:]]+ would match a non-empty string of characters not including any lowercase letters or digits.

Michał Marczyk
A: 

If you ever need to create another regex try reading this. Teaching to fish and all that. :)

Qberticus