Language: EN

regex-clases-de-caracteres

Character Classes in Regex

Character classes are a set of characters that we group together to search for a match with any of the characters in the group.

They are defined using square brackets [], and inside them, we indicate the characters we want to search for.

For example, the expression [abc] will match any of the characters a, b, or c. This means that any string containing at least one of these characters will fulfill the pattern.

Simple Character Classes

We can create simple character classes by simply putting several individual characters into the group. For example:

  • [abc]: Matches any of the characters a, b, or c
  • [aeiou]: Matches any vowel

Let’s see it with an example

Hello, how are you? 123... look mom I'm on TV

By using [ao], we are finding all the letters a or o.

Range of Characters

We can also use a range within the character class to cover a set of contiguous characters.

To do this, we use the separator -. For example:

  • [a-z]: Matches any lowercase letter
  • [A-Z]: Matches any uppercase letter
  • [A-Za-z]: Matches any letter, whether uppercase or lowercase
  • [0-9]: Matches any digit

Let’s see it with an example

Hello, how are you? 123... look mom I'm on TV

Here the pattern [a-f] searches for all the letters from a to f.

Negated Character Classes

We can also negate a character class using the symbol ^ at the beginning of the class.

In this case, the characters that will fulfill the pattern are those not within the range. For example:

  • [^abc]: Matches any character that is not a, b, or c.
  • [^a-zA-Z]: Matches any character that is not a letter.
  • [^0-9]: Matches any character that is not a digit.
Hello, how are you? 123... look mom I'm on TV

In this case, [^a-zA-Z] searches for everything that is not a letter. That is, it finds numbers, periods, and spaces.