...

/

More on Character Classes (Regex)

More on Character Classes (Regex)

This lesson discusses the character classes of Regex in detail.

So far we have used character classes like [aeiou] (listing all allowed characters literally), and [a-z] (specifying a range of characters).

There’s more to these.

One can negate classes by prepending a “not” character ^ inside the square brackets. E.g. [^AEIOUaeiou] allows every character that is not a vowel. So we can find all words that do not start with a vowel like so:

Press + to interact
text = "A regular expression is a sequence of characters that define a search pattern."
p text.scan(/\b[^AEIOUaeiou ][^ ]*\b/)

This means: Start at a word boundary, and allow everything that is not a vowel or a space as a first character, when it is optionally followed by one or many characters that are not a space, followed by a word boundary.

This will output:

Press + to interact
["regular", "sequence", "characters", "that", "define", "search", "pattern"]

Also, regular expressions come with “baked-in”, predefined classes. For example ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy