Check three or more consecutive identical characters or numbers - GeeksforGeeks

01 Feb, 2021
Given string str, the task is to check whether the given string contains 3 or more consecutive identical characters/numbers or not by using Regular Expression. Examples: 
Input: str = “aaa”; Output: true Explanation: The given string contains a, a, a which are consecutive identical characters.Input: str = “abc”; Output: false Explanation: The given string contains a, b, c which are not consecutive identical characters.Input: str = “11”; Output: false Explanation: The given string contains 1, 1 which are not 3 or more consecutive identical numbers. 
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer. 
Get the String.
Create a regular expression to check 3 or more consecutive identical characters or numbers as mentioned below:  
regex = “\\b([a-zA-Z0-9])\\1\\1+\\b”;  
Where: \\b represents the word boundary.( represents the starting of the group 1.[a-zA-Z0-9] represents a letter or a digit.) represents the ending of the group 1.\\1 represents the same character as group 1.\\1+ represents the same character as group 1 one or more times.\\b represents the word boundary.
\\b represents the word boundary.
( represents the starting of the group 1.
[a-zA-Z0-9] represents a letter or a digit.
) represents the ending of the group 1.
\\1 represents the same character as group 1.
\\1+ represents the same character as group 1 one or more times.
\\b represents the word boundary.
Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().Return true if the string matches with the given regular expression, else return false.
Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach: 
C++
Java
Python3
// C++ program to check// three or more consecutive identical// characters or numbers using Regular Expression#include <iostream>#include <regex>using namespace std; // Function to check three or more// consecutive identical characters or numbers.bool isIdentical(string str){   // Regex to check valid three or more  // consecutive identical characters or numbers.  const regex pattern("\\b([a-zA-Z0-9])\\1\\1+\\b");    // If the three or more consecutive  // identical characters or numbers  // is empty return false  if (str.empty())  {     return false;  }   // Return true if the three or more  // consecutive identical characters or numbers  // matched the ReGex  if(regex_match(str, pattern))  {    return true;  }  else  {    return false;  }} // Driver Codeint main(){     // Test Case 1:  string str1 = "aaa";  cout << isIdentical(str1) << endl;   // Test Case 2:  string str2 = "11111";  cout << isIdentical(str2) << endl;   // Test Case 3:  string str3 = "aaab";  cout << isIdentical(str3) << endl;   // Test Case 4:  string str4 = "abc";  cout << isIdentical(str4) << endl;   // Test Case 5:  string str5 = "aa";  cout << isIdentical(str5) << endl;  return 0;} // This code is contributed by yuvraj_chandra
// Java program to check three or// more consecutive identical// characters or numbers// using regular expression import java.util.regex.*;class GFG {     // Function to check three or    // more consecutive identical    // characters or numbers    // using regular expression    public static boolean isIdentical(String str)    {        // Regex to check three or        // more consecutive identical        // characters or numbers        String regex            = "\\b([a-zA-Z0-9])\\1\\1+\\b";         // Compile the ReGex        Pattern p            = Pattern.compile(regex);         // If the string is empty        // return false        if (str == null) {            return false;        }         // Find match between given string        // and regular expression        // using Pattern.matcher()        Matcher m = p.matcher(str);         // Return if the string        // matched the ReGex        return m.matches();    }     // Driver code    public static void main(String args[])    {         // Test Case 1:        String str1 = "aaa";        System.out.println(            isIdentical(str1));         // Test Case 2:        String str2 = "11111";        System.out.println(            isIdentical(str2));         // Test Case 3:        String str3 = "aaab";        System.out.println(            isIdentical(str3));         // Test Case 4:        String str4 = "abc";        System.out.println(            isIdentical(str4));         // Test Case 5:        String str5 = "aa";        System.out.println(            isIdentical(str5));    }}
# Python3 program to check three or# more consecutiveidentical# characters or numbers# using regular expressionimport re # Function to check three or# more consecutiveidentical# characters or numbers# using regular expressiondef isValidGUID(str):     # Regex to check three or    # more consecutive identical    # characters or numbers    regex = "\\b([a-zA-Z0-9])\\1\\1+\\b"         # Compile the ReGex    p = re.compile(regex)     # If the string is empty    # return false    if (str == None):        return False     # Return if the string    # matched the ReGex    if(re.search(p, str)):        return True    else:        return False # Driver code # Test Case 1:str1 = "aaa"print(isValidGUID(str1)) # Test Case 2:str2 = "11111"print(isValidGUID(str2)) # Test Case 3:str3 = "aaab"print(isValidGUID(str3)) # Test Case 4:str4 = "abc"print(isValidGUID(str4)) # Test Case 4:str5 = "aa"print(isValidGUID(str5)) # This code is contributed by avanitrachhadiya2155
true
true
false
false
false
 
avanitrachhadiya2155
yuvraj_chandra
regular-expression
Pattern Searching
Strings
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Check if an URL is valid or not using Regular Expression
Check if a string contains uppercase, lowercase, special characters and numeric values
How to check if string contains only digits in Java
Pattern Searching using Suffix Tree
String matching where one string contains wildcard characters
Reverse a string in Java
Write a program to reverse an array or string
C++ Data Types
Write a program to print all permutations of a given string
Python program to check if a string is palindrome or not