HackerRank Backreferences To Failed Groups Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Backreferences To Failed Groups 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 Backreferences To Failed Groups Solution
HackerRank Backreferences To Failed Groups Solution

HackerRank Backreferences To Failed Groups Solution

Backreference to a capturing group that match nothing is different from backreference to a capturing group that did not participate in the match at all.

Capturing group that match nothing

HackerRank Backreferences To Failed Groups Solution

Here, b? is optional and matches nothing.
Thus, (b?) is successfully matched and capture nothing.
o is matched with o and \1 successfully matches the nothing captured by the group.

Capturing group that didn’t participate in the match at all

HackerRank Backreferences To Failed Groups Solution

In most regex flavors (excluding JavaScript), (b)?o\1 fails to match o.
Here, (b) fails to match at all. Since, the whole group is optional the regex engine does proceed to match o.
The regex engine now arrives at \1 which references a group that did not participate in the match attempt at all.
Thus, the backreference fails to match at all.

Task

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 may have “” separator such that string S gets divided in 4 parts, with each part having exactly two digits. (Eg. 12-34-56-78)

Valid S

12345678
12-3456-87

Invalid S

1-234-56-78
12-45-7810

Note

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 (_________).

HackerRank Backreferences To Failed Groups Solution in Cpp

HackerRank Backreferences To Failed Groups Solution in Java

public class Solution {    

    public static void main(String[] args) {
        
        Regex_Test tester = new Regex_Test();
        tester.checker("^\\d\\d(-?)\\d\\d\\1\\d\\d\\1\\d\\d$"); // Use \\ instead of using \ 
    
    }
}

HackerRank Backreferences To Failed Groups Solution in Python

Regex_Pattern = r"^\d{2}(-?)\d{2}(\1)\d{2}(\1)\d{2}$"	# Do not delete 'r'.

HackerRank Backreferences To Failed Groups Solution in JavaScript

var Regex_Pattern = /^\d\d(-?)\d\d\1\d\d\1\d\d$/; //Do not delete '/'. Replace __________ with your regex. 

HackerRank Backreferences To Failed Groups Solution in PHP

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

Disclaimer: This problem (Backreferences To Failed Groups) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

Sharing Is Caring