Search⌘ K
AI Features

Negative Character Set

Explore how to create and use negative character sets in regular expressions to exclude certain characters or ranges from matches. This lesson helps you understand the caret symbol usage and character ranges to write concise, effective regex patterns. Improve your ability to design regex patterns that specifically avoid unwanted characters to enhance text processing skills.

Character range

Recall the RegEx you wrote in the previous problem /98[12][0123456789]/g. Consider the case when you need to match huge integers or alphabets. Writing all the alphabets one by one in a character set would be very tedious. As a solution, another unique character, the hyphen"-", is used to express the range. Instead of writing digits or alphabets one by one, you can use this range of characters.

A character range represents the range of characters from one start to end.

  • A-Z represents characters from uppercase A to uppercase Z.
  • a-z represents characters from lowercase a to lowercase z.
  • 0-9 represents numbers from 0 to 9.

You can use this range character inside the character set as well. And that is the reason why you should escape the hyphen "-" inside the character set.

To write the above-mentioned RegEx more concisely, use the range character. And the RegEx will become ...