Using negative character class [^...]


What is a negative character class? How to specify a negative character class in a regular expression?

How to use negative character class to capture words that contains no vowel letters in this lyrics: Daddy started out in San Francisco, Tootin' on his trumpet loud and mean, Suddenly a voice said, "Go forth Daddy, Spread the picture on a wider screen." And the voice said, "Brother, there's a million pigeons Ready to be hooked on new religions. Hit the road, Daddy, leave your common-law wife. Spread the religion of The Rhythm Of Life."?



A negative character class defines a set of characters to be excluded in the match process. A negative character class is expressed with '^' as the first character in a character class, [^...].

A negative character class matches any character except those character but those listed in the character class.

Here are some examples of negative character classes:

[^0-9]     # matches a non-numeric character
[^bcr ]at  # matches any '.at', but not 'bat', 'cat' and 'rat'

The regular expression to a capture words without vowel letters is:


\b - word boundary
[^aeiou\W] - any non-vowel and non-word letter
{2,} - repeating 2 or more times

Click the button to test this regular expression here online:

2013-02-02, 0👍, 0💬