Match Expression where a single special character in pattern can match one or more characters | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int regexMatch ( string text , string pat ) { int lenText = text . length ( ) ; int letPat = pat . length ( ) ; int i = 0 , j = 0 ; while ( i < letPat ) { 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 ) ; } int main ( ) { string str = " ABABABA " ; string pat = " A # B # A " ; if ( regexMatch ( str , pat ) ) cout << " yes " ; else cout << " no " ; return 0 ; }