Hello Programmers, In this post, you will learn how to solve HackerRank Matching {x} Repetitions Solution. This problem is a part of the Regex HackerRank Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the Regex HackerRank Solutions using CPP, JAVA, PYTHON, JavaScript & PHP Programming Languages.

HackerRank Matching {x} Repetitions Solution
{x}
The tool {x} will match exactly x repetitions of character/character class/groups.

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
You have a test string S.
Your task is to write a regex that will match S using the following conditions:
- S must be of length equal to
45
. - The first 40 characters should consist of
letters
(both lowercase and uppercase), or ofeven digits
. - The last 5 characters should consist of
odd digits
orwhitespace characters
.
Note
This is a regex only challenge. You are not required to write any code.
You have to fill the regex pattern in the blank (_________
).
HackerRank Matching {x} Repetitions Solution in Cpp
HackerRank Matching {x} Repetitions Solution in Java
public class Solution { public static void main(String[] args) { Regex_Test tester = new Regex_Test(); tester.checker("^[a-zA-Z02468]{40}(?:[13579]|\\s){5}$"); // Use \\ instead of using \ } }
HackerRank Matching {x} Repetitions Solution in Python
Regex_Pattern = r'^[a-zA-Z02468]{40}[\s13579]{5}$' # Do not delete 'r'.
HackerRank Matching {x} Repetitions Solution in JavaScript
var Regex_Pattern = /^[a-zA-Z02468]{40}[13579\s]{5}$/;
HackerRank Matching {x} Repetitions Solution in PHP
$Regex_Pattern = "/^[a-zA-Z02468]{40}[\s13579]{5}$/"; //Do not delete '/'. Replace __________ with your regex.
Disclaimer: This problem (Matching {x} Repetitions) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.