题目详情
字符串匹配问题,给定一串字符串,按照指定规则对其进行匹配,并将匹配的结果保存至output数组中,多个匹配项用空格间隔,最后一个不需要空格。
要求:
- 匹配规则中包含通配符?和*,其中?表示匹配任意一个字符,*表示匹配任意多个(>=0)字符。
- 匹配规则要求匹配最大的字符子串,例如a*d,匹配abbdd而非abbd,即最大匹配子串。
- 匹配后的输入串不再进行匹配,从当前匹配后的字符串重新匹配其他字符串。
要求实现函数:char* my_find(char input[], char rule[])
举例说明:
input:abcadefg
rule:a?c
output:abc
input :newsadfanewfdadsf
rule: new
output: new new
input :breakfastfood
rule: f*d
output:fastfood
代码:
// Regexpr.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#include <assert.h>
char *g_pStack[200];
int g_nDeep = 0;
char* my_find(char input[], char rule[]);
int Match(char *p, char *&pattern, char **pEnd);
int SearchAllChar(char *p, char ch);
int main(int argc, char* argv[])
{
char *output = NULL;
if (output = my_find("akfasdofoodx", "f*do"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "fasdo"));
delete output;
if (output = my_find("akfasdofoodx", "a*fa*d"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "akfasdofood"));
delete output;
if (output = my_find("akfasdofoodx", "a*fa*do"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "akfasdo"));
delete output;
if (output = my_find("akfasdofoodx", "a**f**d"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "akfasdofood"));
delete output;
if (output = my_find("akfasdofoodx", "a**f**x"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "akfasdofoodx"));
delete output;
if (output = my_find("akfasdofoodx", "**f**x"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "akfasdofoodx"));
delete output;
if (output = my_find("akfasdofoodx", "**f**"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "akfasdofoodx"));
delete output;
if (output = my_find("akfasdofoodx", "f*dx"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "fasdofoodx"));
delete output;
if (output = my_find("abcadcfg", "a?c"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "abc adc"));
delete output;
if (output = my_find("abcadcfg", "a?*c"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "abcadc"));
delete output;
if (output = my_find("newsadfanewfdadsf", "new"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "new new"));
delete output;
if (output = my_find("newsadfaneswfdadsf", "new*d*s"))
printf("%s\r\n", output);
assert(0 == strcmp(output, "newsadfaneswfdads"));
delete output;
return 0;
}
void mystrncpy(char *pDest, char *pScr, int n)
{
int i;
for (i = 0; i < n; i++)
{
pDest[i] = pScr[i];
}
pDest[i] = ' ';
}
char* my_find(char input[], char rule[])
{
printf("Text is:\"%s\", find pattern:\"%s\"\r\n", input, rule);
printf(" result ");
int nFind = 0;
char *pattern = rule;
int nIdx = 0;
int nLen = 0;
char *pStart = input;
while('\0' != *pStart++)
nLen++;
pStart = input;
char *output = new char[nLen+1];
while ('\0' != *pStart)
{
char *pEnd = NULL;
int bMatch = Match(pStart, pattern, &pEnd);
if (bMatch)
{
mystrncpy(&output[nIdx], pStart, pEnd-pStart);
nIdx += pEnd-pStart+1;
nFind++;
pStart = pEnd;
if ('*' == *(pattern-1))
{
nIdx--;
while (*pStart)
{
output[nIdx++] = *pStart++;
}
output[nIdx++] = '\0';
break;
}
}
else
{
pStart++;
}
pattern = rule;
}
if (0 != nFind)
{
output[nIdx-1] = 0;
}
return output;
}
int Match(char *p, char *&pattern, char **pEnd)
{
int bPreWildchar = 0;
int nFind = NULL;
while ('*' == *pattern)
{
bPreWildchar = 1;
pattern++;
}
*pEnd = p;
if ('\0' == *pattern)
{
return 1;
}
if ('\0' == *p)
{
return 0;
}
if (bPreWildchar)
{
nFind = SearchAllChar(p, *pattern);
while (0 != nFind--)
{
p = g_pStack[--g_nDeep];
char * tmp = pattern;
if (Match(++p, ++pattern, pEnd))
return 1;
pattern = tmp;
}
return 0;
}
if ('?' == *pattern || *pattern == *p)
{
return Match(p+1, ++pattern, pEnd);
}
return 0;
}
int SearchAllChar(char *p, char ch)
{
int nFind = 0;
while (*p)
{
if (ch == *p)
{
g_pStack[g_nDeep++] = p;
nFind++;
}
p++;
}
return nFind;
}
Text is:"akfasdofoodx", find pattern:"f*do"
result fasdo
Text is:"akfasdofoodx", find pattern:"a*fa*d"
result akfasdofood
Text is:"akfasdofoodx", find pattern:"a*fa*do"
result akfasdo
Text is:"akfasdofoodx", find pattern:"a**f**d"
result akfasdofood
Text is:"akfasdofoodx", find pattern:"a**f**x"
result akfasdofoodx
Text is:"akfasdofoodx", find pattern:"**f**x"
result akfasdofoodx
Text is:"akfasdofoodx", find pattern:"**f**"
result akfasdofoodx
Text is:"akfasdofoodx", find pattern:"f*dx"
result fasdofoodx
Text is:"abcadcfg", find pattern:"a?c"
result abc adc
Text is:"abcadcfg", find pattern:"a?*c"
result abcadc
Text is:"newsadfanewfdadsf", find pattern:"new"
result new new
Text is:"newsadfaneswfdadsf", find pattern:"new*d*s"
result newsadfaneswfdads