Negative Lookbehind – HackerRank Solution

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



The negative lookbehind (?<!) asserts regex_1 not to be immediately preceded by regex_2. Lookbehind is excluded from the match (do not consume matches of regex_2), but only assert whether a match is possible or not.


You have a test String S.
Write a regex which can match all the occurences of characters which are not immediately preceded by vowels (a, e, i, u, o, A, E, I, O, U).


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

JavaScript do not support lookbehind.

Solution – Negative Lookbehind – HackerRank Solution


$Regex_Pattern = '/(?<![aeiouAEIOU])[\s\S]/'; //Do not delete '/'. Replace __________ with your regex. 

$handle = fopen ("php://stdin","r");
$Test_String = fgets($handle);

preg_match_all($Regex_Pattern, $Test_String, $output_array);
printf("Number of matches : %d",count($output_array[0]));

Note: This problem (Negative Lookbehind) 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 *