代码之美《正则匹配》
原创
©著作权归作者所有:来自51CTO博客作者kinkding的原创作品,请联系作者获取转载授权,否则将追究法律责任
程序如下:
int match(
char * regexp,
char *text);
int matchhere(
char * regexp,
char *text);
int matchstar(
int c,
char *regexp,
char *text);
int main(
int argc,
char* argv[])
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
{
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
printf(
"Hello World!\n");
int t=match(
"t.a*$",
"twaaaaw");
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
printf(
"result :%d \n",t);
return 0;
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
/* match: search for regexp anywhere in text */
int match(
char *regexp,
char *text)
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do {
/* must look even if string is empty */
if (matchhere(regexp, text))
return 1;
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
while (*text++ != '\0');
return 0;
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
/* matchhere: search for regexp at beginning of text */
int matchhere(
char *regexp,
char *text)
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
{
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
printf(
"2 %s,%s\n",regexp,text);
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*') {
//printf("%c,%s,%s\n",regexp[0],regexp+2,text);
return matchstar(regexp[0], regexp+2, text);
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
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;
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
/* matchstar: search for c*regexp at beginning of text */
int matchstar(
int c,
char *regexp,
char *text)
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
{
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
printf(
"3 %c,%s,%s\n",c,regexp,text);
do {
/* a * matches zero or more instances */
if (matchhere(regexp, text))
return 1;
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
![代码之美《正则匹配》_休闲](https://s2.51cto.com/images/editer/InBlock.gif)
}
打印出来的文本:
2 t.a*$,twaaaaw
2 .a*$,waaaaw
2 a*$,aaaaw
3 a,$,aaaaw
2 $,aaaaw
2 $,aaaw
2 $,aaw
2 $,aw
2 $,w
2 t.a*$,waaaaw
2 t.a*$,aaaaw
2 t.a*$,aaaw
2 t.a*$,aaw
2 t.a*$,aw
2 t.a*$,w
2 t.a*$,
result :0
转换成为JAVA:
public int match(String regexp, String text) {
if (regexp.charAt(0) == '^') {
return matchhere(substr(regexp, 1), text);
}
int i = 0;
do {
text = substr(text, i++ > 0 ? 1 : 0);
if (matchhere(regexp, text) == 1) {
return 1;
}
} while (text.charAt(0) != '\0');
return 0;
}
public int matchhere(String regexp, String text) {
System.out.println("2 " + regexp + "," + text);
char c = regexp.charAt(0);
if (c == '\0') {
return 1;
}
if (regexp.length() > 1 && regexp.charAt(1) == '*') {
return matchstar(c, substr(regexp, 2), text);
}
if (c == '$' && regexp.length() == 1) {
return text.charAt(0) == '\0' ? 1 : 0;
}
if (text != "\0" && (c == '.' || text.charAt(0) == c)) {
return matchhere(substr(regexp, 1), substr(text, 1));
}
return 0;
}
public int matchstar(char c, String regexp, String text) {
System.out.println("3 " + c + "," + regexp + "," + text);
int i = 0;
do {
text = substr(text, i++ > 0 ? 1 : 0);
if (matchhere(regexp, text) == 1) {
return 1;
}
} while (text.charAt(0) != '\0' && (c == '.' || text.charAt(0) == c));
return 0;
}
private static String substr(String s, int i) {
if (s != null && s.length() > i) {
return s.substring(i);
} else {
return "\0";
}
}