HackerRank - Regex - Character Class

© HackerRank

Matching Specific Characters

The character class [ ] matches only one out of several characters placed inside the square brackets.

Task

1203x.

1
^[123][120][xs0][30Aa][xsu][\.\,]$

Excluding Specific Characters

The negated character class [^] matches any character that is not in the square brackets.

Task

think?

1
^[^\d][^aeiou][^bcDF][^\s][^AEIOU][^\.\,]$

Matching Character Ranges

In the context of a regular expression (RegEx), a character class is a set of characters enclosed within square brackets that allows you to match one character in the set.

A hyphen (-) inside a character class specifies a range of characters where the left and right operands are the respective lower and upper bounds of the range. For example:

  • [a-z] is the same as [abcdefghijklmnopqrstuvwxyz]
  • [A-Z] is the same as [ABCDEFGHIJKLMNOPQRSTUVWXYZ]
  • [0-9] is the same as [0123456789]

In addition, if you use a caret (^) as the first character inside a character class, it will match anything that is not in that range. For example, [^0-9] matches any character that is not a digit in the inclusive range from 0 to 9.

It’s important to note that, when used outside of (immediately preceding) a character or character class, the caret matches the first character in the string against that character or set of characters.

Task

h4CkR

1
^[a-z][1-9][^a-z][^A-Z][A-Z]*

Resources

Regex - Subdomains - Character Class