Ruby - Regular expressions character classes

Introduction

Regular expressions character classes allow you to match against a specific set of characters.

For example, you can scan through all the vowels in a string:

Demo

"This is a test".scan(/[aeiou]/) { |x| puts x }

Result

[aeiou] means "match any of a, e, i, o, or u."

You can also specify ranges of characters inside the square brackets, like so:

Demo

"This is a test".scan(/[a-m]/) { |x| puts x }

Result

This scan matches all lowercase letters between a and m.