HackerRank - Regex - Repetitions

© HackerRank

Matching {x} Repetitions

The tool {x} will match exactly x repetitions of character/character class/groups.

For Example

  • w{3} : It will match the character w exactly 3 times.
  • [xyz]{5} : It will match the string of length 5 consisting of characters {x, y, z}. For example it will match xxxxx, xxxyy and xyxyz.
  • \d{4} : It will match any digit exactly 4 times.

Task

2222222222aaaaaaaaaa2222222222aaaaaaaaaa13 57

1
^[a-zA-Z02468]{40}[13579\s]{5}$

Matching {x, y} Repetitions

The {x,y} tool will match between x and y (both inclusive) repetitions of character/character class/group.

For Example

  • w{3,5} : It will match the character w 3, 4 or 5 times.
  • [xyz]{5,} : It will match the character x, y or z 5 or more times.
  • \d{1, 4} : It will match any digits 1, 2, 3, or 4 times.

Task

3threeormorealphabets.

1
^\d{1,2}[a-zA-Z]{3,}\.{0,3}$

Matching Zero Or More Repetitions

The * tool will match zero or more repetitions of character/character class/group.

For Example

  • w* : It will match the character w 0 or more times.
  • [xyz]* : It will match the characters x, y or z 0 or more times.
  • \d* : It will match any digit 0 or more times.

Task

14

1
^\d{2,}[a-z]*[A-Z]*$

Matching One Or More Repetitions

The + tool will match one or more repetitions of character/character class/group.

For Example

  • w+ : It will match the character w 1 or more times.
  • [xyz]+ : It will match the character x, y or z 1 or more times.
  • \d+ : It will match any digit 1 or more times.

Task

1Qa

1
^\d+[A-Z]+[a-z]+$

Matching Ending Items

The $ boundary matcher matches an occurrence of a character/character class/group at the end of a line.

Task

Kites

1
^[a-zA-Z]*s$

Resources

Regex - Subdomains - Repetitions