here is code for regular expression matching
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int matchhere(char *regexp,char *text);
int matchstar(int c,char *regexp,char *text);
int match(char *regexp,char *text)
{
if(regexp[0]=='^')
return matchhere(regexp+1,text);
do {
if(matchhere(regexp,text))
return 1;
} while(*text++!='\0');
return 0;
}
int matchhere(char *regexp,char *text)
{
if(regexp[0]='\0')
return 1;
if(regexp[1]='*')
return matchstar(regexp[0],regexp+2,text);
if(regexp[0]=='$' && regexp[1]=='\0')
return *text=='\0';
if(*text!='\0' && regexp[0]=='.' || regexp[0]==*text)
return matchhere(regexp+1,text+1);
return 0;
}
int main()
{
char *regexp="^abb";
char *text="cabbacd";
cout<<match(regexp,text)<<endl;
return 0;
}
int matchstar(int c,char *regexp,char *text)
{
do {
if(matchhere(regexp,text))
return 1;
}
while(*text!='\0' && (*text++==c || c=='.'));
return 0;
}
it writes runtime error,after debuging,i have got following result
+ regexp 0x00365839 "abb" char *
regexp[0] 97 'a' char
+ text 0x00365830 "cabbacd" char *
why?please help me,for clarify,i can't debug more,because it gives me unexpections error with window
if(regexp[0]='\0')
return 1;
if(regexp[1]='*')
These assignments aren't just logically wrong, they're illegal. It is illegal to mutate a string literal. If you would use const char*
, this problem would have been caught for you by the compiler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.