C++正则表达式 全家老少一波流,所有财富都给你 只要帮我解决~~~

Regular expressions are one of the most broadly applicable specialized languages, a compact and expressive notation for describing patterns of text. Regular expressions are algorithmically interesting, easy to implement in their simpler forms, and very useful.A regular expression is a sequence of characters that defines a pattern. Most characters in the pattern simply match themselves in a target string, so the regular expression "abc" matches that sequence of three letters wherever it occurs in the target. A few characters are used in patterns as metacharacters to indicate repetition, grouping, or positioning. In POSIX regular expressions, "^" stands for the beginning of a string and "$" for the end, so "^x" matches an "x" only at the beginning of a string, "x$" matches an "x" only at the end, "^x$" matches "x" only if it is the sole character of the string, and "^$" matches the empty string. The character "." (a period) matches any character, so "x.y" matches "xay," "x2y," and so on, but not "xy" or "xyxy." The regular expression "^.$" matches a string that contains any single character.Please implement a simple Regular Expression matching program which only supports the three metacharacters, "^", "$", and ".". The program receives two strings (splited by a whitespace) in each line: the first one is a target string (without whitespace), and the second one is a regular expression pattern (without whitespace). If the pattern matches the string, then it prints "hit" in one line, else prints "lost". Sample Input (with multiple lines):abcdef abcabcdef ^abcabcdef abc$abcdef a.cabcdef ^a.c$abcdef ^a.babcdef ^abcdef$Sample Output:hithitlosthitlostlosthit
2026年09月27日 09:09
有2个网友回答
网友(1):

题意:"^":必须对应字符串的开头

      "$":必须对应字符串的末尾

      ".":必须对应一个字符(任意一个字符)

程序:

#include "stdafx.h"

#include 

using namespace std;

#include 

//从arrayF的第bg(从0开始算起)个位置开始找与m相同的元素,返回位置

int FindMatchLoc(char* &_arrayF,char m,int bg)

{

 char* arrayF = new char[];

 arrayF = _arrayF;

 int curF = bg;

 while ((curF

 {

  curF++;

 }

 return curF;

 delete []arrayF;

}

//从_org的第curO(从0开始计算)个位置开始,从_match的第curM(从0开始计算)个位置开始,进行匹配

//成功返回1 反之返回0

bool Match(char* &_org,char* &_match,int curO,int curM)

{

 char* org = new char[];

 char* match = new char[];

 org = _org;

 match =  _match;

 while ((curO

 {

  if (match[curM] == '.')

  {

   curM ++;

   curO ++;

  }

  else if (match[curM] == org[curO])

  {

   curM ++;

   curO ++;

  }

  else if ((match[curM] == '$')&&curO == strlen(org))

  {

   return true;

  }

  else

  {

   return false;

  }

 }

 

 if (curM ==strlen(match)||match[curM] == '$')

 {

  return true;

 }

 return false;

 delete []org;

 delete []match;

}

//正则表达式的主调用函数

bool RegularExpress(char* &_org,char* &_match)

{

 char *org = new char[];

 char *match = new char[];

 org = _org;

 match  = _match;

 if((strlen(org) == 0)||(strlen(match) == 0))

 {

  return false;

 }

 else

 {

  int curM = 0;

  int curO = 0;

  bool canS = false;//是否还可能进行下次查找。如abcdad 中找ad中的a

  if (match[0] == '^')

  {

   curM = 1;

   curO = 0;

   canS = false;

   return Match(org,match,curO,curM);

  }

  else if(match[0] == '.')

  {

   curM = 1;

   canS = true;

   int bg = 1;

   while(canS)

   {

    curO = FindMatchLoc(_org,match[1],bg);

    if(Match(org,match,curO,curM))

    {

     return true;

    }

    else

    {

     bg = curO + 1;

     if (bg

     {

      canS = true;

     }

     else

     {

      canS = false;

     }

    }

   }

  } 

  else if (match[0] == '$')

  {

   return false;

  }

  else

  {

   curM = 0;

   canS = true;

   int bg = 0;

   while(canS)

   {

    curO = FindMatchLoc(_org,match[0],bg);

    if(Match(org,match,curO,curM))

    {

     return true;

    }

    else

    {

     bg = curO + 1;

     if (bg

     {

      canS = true;

     }

     else

     {

      canS = false;

     }

    }

   }

  }

 }

 return false;

 delete []org;

 delete []match;

}

//主函数

void main()

{   char* strOrg;

 char* strMatch;

 while(1)

 {

  strOrg = new char[];

  strMatch = new char[];

  cin>>strOrg;

  cin>>strMatch;

  if(RegularExpress(strOrg ,strMatch))

  {

   cout<<"hit"<

  }

  else

  {

   cout<<"lost"<

  }

 }

 delete []strOrg;

 delete []strMatch;

 

}

结果显示:(见图)

思路:主要是将要match的式子的形式进行分类,和strOrg的匹配都是相同的:挨个字符进行。

(*^__^*)  加油~~

网友(2):

下面是我写的程序,初步测试通过。请参考,有问题hi我。
很辛苦,希望能加点分:)

#include "stdafx.h"
#include
#include

#define MAX 20
//flag=0,front
//flag=1,reverse
int compare(int flag, char *a, char *b, int begin, int end)
{
if(!flag)
{
for(int i=begin; i<=end; i++)
{
if(b[i] == '.')
{
continue;
}
if(b[i] != a[i-begin])
{
return 0;
}
}
}
else
{
int l = strlen(a);
for(int i=begin; i>=end; i--)
{
if(b[i] == '.')
{
continue;
}
if(b[i] != a[l-begin+i-1])
{
return 0;
}
}
}

return 1;
}

int main(void)
{
char a[MAX],b[MAX];
int lena,lenb;
int res=0;

printf("input 2 strings, or e e to quit:\n");
while(1)
{
scanf("%s%s", a, b);

if((a[0] == 'e') && (b[0] == 'e'))
return 0;
//check,0:lost,1:hit
if(strcmp(b, "^") && strcmp(b, "$") && strcmp(b, "$^"))
{
lena = strlen(a);
lenb = strlen(b);
if((b[0] == '^') && (b[lenb-1] == '$'))
{
//compare all
if(lenb == (lena+2))
{
res = compare(0, a, b, 1, lenb-2);
}
else
{
res = 0;
}
}
else if(b[lenb-1] == '$')
{
//compare reverse
if(lenb <= (lena+1))
{
res = compare(1, a, b, lenb-2, 0);
}
else
{
res = 0;
}
}
else if(b[0] == '^')
{
//compare head
if(lenb <= (lena+1))
{
res = compare(0, a, b, 1, lenb-1);
}
else
{
res = 0;
}
}
else
{
//compare head
if(lenb <= lena)
{
res = compare(0, a, b, 0, lenb-1);
}
else
{
res = 0;
}
}

if(res)
{
printf("hit\n");
}
else
{
printf("lost\n");
}
}
else
{
printf("invalid regular expression\n");
}
}

return 0;
}