Check three or more consecutive identical characters or numbers | Java program to check three or more consecutive identical characters or numbers using regular expression ; Function to check three or more consecutive identical characters or numbers using regular expression ; Regex to check three or more consecutive identical characters or numbers ; Compile the ReGex ; If the string is empty return false ; Find match between given string and regular expression using Pattern . matcher ( ) ; Return if the string matched the ReGex ; Driver code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import java . util . regex . * ; class GFG { public static boolean isIdentical ( String str ) { String regex = " \\ b ( [ a - zA - Z0-9 ] ) \\ 1 \\ 1 + \\ b " ; Pattern p = Pattern . compile ( regex ) ; if ( str == null ) { return false ; } Matcher m = p . matcher ( str ) ; return m . matches ( ) ; } public static void main ( String args [ ] ) { String str1 = " aaa " ; System . out . println ( isIdentical ( str1 ) ) ; String str2 = "11111" ; System . out . println ( isIdentical ( str2 ) ) ; String str3 = " aaab " ; System . out . println ( isIdentical ( str3 ) ) ; String str4 = " abc " ; System . out . println ( isIdentical ( str4 ) ) ; String str5 = " aa " ; System . out . println ( isIdentical ( str5 ) ) ; } }