Hello Programmers, In this post, you will learn how to solve HackerRank Negative Lookahead 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 Negative Lookahead Solution
regex_1(?!regex_2)
The negative lookahead (?!) asserts regex_1
not to be immediately followed by regex_2
. Lookahead is excluded from the match (do not consume matches of regex_2
), but only assert whether a match is possible or not.

Task
You have a test String S.
Write a regex which can match all characters which are not immediately followed by that same character.
Example
If S = goooo, then regex should match g
oooo
. Because the first g
is not follwed by g and the last o
is not followed by o.
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 Negative Lookahead Solution in Cpp
HackerRank Negative Lookahead Solution in Java
public class Solution { public static void main(String[] args) { Regex_Test tester = new Regex_Test(); tester.checker("(?!(\\S)\\1)(\\S)"); //Use '\\' instead of '\'. } }
HackerRank Negative Lookahead Solution in Python
Regex_Pattern = r"(.)(?!\1)" # Do not delete 'r'.
Negative Lookahead Solution in JavaScript
var Regex_Pattern = /(.)(?!\1)/g; //Do not delete `/` and `/g`.
Negative Lookahead Solution in PHP
$Regex_Pattern = '/(\D)(?!\1)/'; //Do not delete '/'. Replace __________ with your regex.
Disclaimer: This problem (Negative Lookahead) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.