Branch Reset Groups – HackerRank Solution

In this post, we will solve Branch Reset Groups HackerRank Solution. This problem (Branch Reset Groups) is a part of HackerRank Regex series.


NOTE – Branch reset group is supported by Perl, PHP, Delphi and R.

branch reset group consists of alternations and capturing groups. (?|(regex1)|(regex2))
Alternatives in branch reset group share same capturing group.


You have a test string S.
Your task is to write a regex which will match S, with following condition(s):

  • S consists of 8 digits.
  • S must have “—“, “-“, “.” or “:” separator such that string S gets divided in 4 parts, with each part having exactly two digits.
  • S string must have exactly one kind of separator.
  • Separators must have integers on both sides.

Valid S


Invalid S



This is a regex only challenge. You are not required to write any code.
You only have to fill the regex pattern in the blank (_________).

Branch Reset Groups – HackerRank Solution


$Regex_Pattern = '/^\d{2}(-(?:--)?|\.|:)\d{2}\1\d{2}\1\d{2}$/'; //Do not delete '/'. Replace __________ with your regex. 

$handle = fopen ("php://stdin","r");
$Test_String = fgets($handle);
if(preg_match($Regex_Pattern, $Test_String, $output_array)){
    print ("true");
} else {
    print ("false");


Note: This problem (Branch Reset Groups) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *