Expression Pattern matching
/***
Implement
regular expression matching with support for ‘.’ and ‘*’.
"."
can be any single character
"x*"
can represent any # of preceding character, eg
"xxx","xx"...
1.
API: s and p are char linked list?
2.
both s and p can contain matching character".""*", or just
p
if
*s = *p (check next p is '*')
***/
#include <iostream>
#include <string>
using namespace std;
bool isMatch(const char *s, const char *p) {
//base
case
if(*s == '\0' && *p == '\0')
return true;
// p
next is not '*'
if(*(p+1) != '*') {
if(*s != *p && *p != '.' || *s == '\0')
return false;
else return isMatch(s+1, p+1);
}
// p
next is '*'
while((*p == *s) || (*p == '.' && *s != '\0')) {
if(isMatch(s, p+2))
return true;
s++;
}
return isMatch(s, p+2);
}
void main() {
string s = "aab";
string p = ".b*.*b";
if (isMatch(&s[0], &p[0]))
{
cout<<"match expression"<<endl;
} else cout<< "not
match"<<endl;
}
Comments
Post a Comment