Match Expression where a single special character in pattern can match one or more characters | Java program for pattern matching where a single special character can match one more characters ; Returns true if pat matches with text . ; i is used as an index in pattern and j as an index in text . ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text . ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static boolean regexMatch ( String text , String pat ) { int lenText = text . length ( ) ; int lenPat = pat . length ( ) ; char [ ] Text = text . toCharArray ( ) ; char [ ] Pat = pat . toCharArray ( ) ; int i = 0 , j = 0 ; while ( i < lenPat ) { if ( Pat [ i ] != ' # ' ) { if ( Pat [ i ] != Text [ j ] ) return false ; i ++ ; j ++ ; } else { j ++ ; while ( Text [ j ] != Pat [ i + 1 ] ) j ++ ; i ++ ; } } return ( j == lenText ) ; } public static void main ( String [ ] args ) { String str = " ABABABA " ; String pat = " A # B # A " ; if ( regexMatch ( str , pat ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }