HackerRank Capturing & Non-Capturing Groups Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Capturing & Non-Capturing Groups Solution. This problem is a part of the Regex HackerRank Series.

HackerRank Capturing & Non-Capturing Groups Solution
HackerRank Capturing & Non-Capturing Groups Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Capturing & Non-Capturing Groups Solution

()
Parenthesis ( ) around a regular expression can group that part of regex together. This allows us to apply different quantifiers to that group.

These parenthesis also create a numbered capturing. It stores the part of string matched by the part of regex inside parentheses.

These numbered capturing can be used for backreferences. ( We shall learn about it later )

(?: )
(?: ) can be used to create a non-capturing group. It is useful if we do not need the group to capture its match.

Task

You have a test String S.
Your task is to write a regex which will match S with the following condition:

  • S should have 3 or more consecutive repetitions of ok.

Note

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

HackerRank Capturing & Non-Capturing Groups Solutions in Cpp

HackerRank Capturing & Non-Capturing Groups Solutions in Java

public class Solution {    
    public static void main(String[] args) {
        
        Regex_Test tester = new Regex_Test();
        tester.checker("((?:ok){3,})");
    }
}

HackerRank Capturing & Non-Capturing Groups Solutions in Python

Regex_Pattern = r'(ok){3,}'   # Do not delete 'r'.

HackerRank Capturing & Non-Capturing Groups Solutions in JavaScript

var Regex_Pattern = /(?:ok){3}/; //Do not delete '/'. Replace __________ with your regex. 

HackerRank Capturing & Non-Capturing Groups Solutions in PHP

$Regex_Pattern = "/(ok){3,}/"; //Do not delete '/'. Replace __________ with your regex. 

Disclaimer: This problem (Capturing & Non-Capturing Groups) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Alternative Matching Solution

Leave a Reply

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