这是较为简单的正则表达式引擎
目前只支持. + | 三种特殊字符
发现bug 请联系
已有封装好的函数,请直接使用
未来将逐渐更新

例子如图
// regulatr.cpp : 定义控制台应用程序的入口点。
//

/*______________________________________________________
regular expression engine
written by 唐hz
2015.8.10
_______________________________________________________*/
#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;
struct State;
#define error -1//此处不确定
#define normal 1
#define finish 16
#define mul_way 2
#define start 8
#define any 32
#define error_back 64
#define self_loop1 4
struct Linker{
State *from;
State *to;
char request;
};
struct LinkerList{
Linker l;
struct LinkerList *next;
};
struct State{
int status;
int position;
LinkerList * in;
LinkerList * out;
};
struct Position{
State* p;
int i;
};

State g_State[100];
int g_next[100];
int g_start,g_finish,g_maxi=-1;
int g_can_not_any=-1;

void next(char *p){
int n=strlen(p);
int i=1,j=0;
g_next[j]=-1;
while(i<n){
if(p[i]==p[j])
g_next[i++]=j++;
else {
if(j!=0)
j=g_next[j-1]+1;
else{
j=0;
g_next[i++]=-1;
}
}

}
}

void addlinker(LinkerList **a,char b,State *c,State *d){
if(*a==NULL){
(*a)=new LinkerList;
(*a)->l.request=b;
(*a)->l.from=c;
(*a)->l.to=d;
(*a)->next=NULL;
return ;
}
LinkerList * temp=*a;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new LinkerList;
temp=temp->next;
temp->l.request=b;
temp->l.from=c;
temp->l.to=d;
temp->next=NULL;
return ;
}
void addlinker2(LinkerList **a,State *d){
if(*a==NULL){
(*a)=new LinkerList;
(*a)->l.request=b;
(*a)->l.from=c;
(*a)->l.to=d;
(*a)->next=NULL;
return ;
}
LinkerList * temp=*a,*temp1;
while(temp->next&&temp->next->next!=NULL){
temp=temp->next;
}
temp1=temp->next;
temp->next=new LinkerList;
temp=temp->next;
temp->l.request=b;
temp->l.from=c;
temp->l.to=d;
temp->next=temp1;
return ;
}
void addlinker1(LinkerList **a,State *d){
if(*a==NULL){
(*a)=new LinkerList;
(*a)->l.request=b;
(*a)->l.from=c;
(*a)->l.to=d;
(*a)->next=NULL;
return ;
}
LinkerList * temp;
temp=new LinkerList;
temp->l.request=b;
temp->l.from=c;
temp->l.to=d;
temp->next=*a;
*a=temp;
return ;
}
void connect(State &a,State &b,char r,int x=0){
if(x==0){
addlinker(&(a.out),r,&a,&b);
addlinker(&(b.in),&b);}
else if(x==1){
addlinker1(&(a.out),&b);
addlinker1(&(b.in),&b);
}
else if(x==2){
addlinker2(&(a.out),&b);
addlinker2(&(b.in),&b);
}
}

void compile(char *p,char*q){
int i=0,j=0;
while(i<(int)strlen(q)){
if(q[i]=='|'){
i+=2;
}
else if(q[i]=='+'){
i++;
}
else p[j++]=q[i++];
}
p[j]='\0';
}

void construction(char *p){
char q[100];
compile(q,p);
next(q);
int i=0;
for(int j=0;j<100;j++){
g_State[j].in=g_State[j].out=NULL;
g_State[j].status=0;//
g_State[j].position=j;
}
g_State[0].status|=start;
connect(g_State[0],g_State[0],error,0);
while(*p!='\n')
{
switch(*p){
case '|':
if(i>0) i--;
p++;
g_State[i].status|=mul_way;
break;
case '+':
i--;
p--;
connect(g_State[i],g_State[i],*p,2);
g_State[i].status|=self_loop1;
i++;
p+=2;
break;
default:
if(!(g_State[i].status&start)&&!(g_State[i].status&error_back)){
g_State[i].status|=error_back;
connect(g_State[i],g_State[g_next[i-1]+1],0);}
if(*p=='.'){
g_State[i].status|=any;
}
connect(g_State[i],g_State[i+1],1);
g_State[i].status|=normal;
i++;
p++;}
}
g_State[i].status|=finish;

}//查int 字节
void realease(){
LinkerList *temp,*temp1;
for(int i=0;i<100;i++)
{
temp=g_State[i].in;
while(temp){
temp1=temp;
temp=temp->next;
delete temp1;
}
temp=g_State[i].out;
while(temp){
temp1=temp;
temp=temp->next;
delete temp1;
}
}
}

bool NextState(Position a,char *p,vector<Position> & find,int i){
auto temp=a.p->out;
Position rec;
bool j=false;
while(temp){
if(temp->l.request!=error&&temp->l.request==*(p+i)||(temp->l.request=='.'&&a.p->status&any&&a.p->position!=g_can_not_any)){
j=true;
if(temp->l.to->position==a.p->position&&temp->l.request!='.'&&a.p->status&any){
g_can_not_any=a.p->position;
}
rec.p=((temp->l).to);
rec.i=i+1;
if(rec.i<(int)strlen(p))
find.push_back(rec);
}
else
{
if((temp->l.request==error)&&(!(a.p->status&finish))&&!j){
if(((temp->l).to)->position<g_can_not_any){
cout<<"使g_can_not_any回复初值\n";
g_can_not_any=-1;}
rec.p=((temp->l).to);
rec.i=i;
if(!(rec.p->status&start))
find.push_back(rec);
}
}
temp=temp->next;
}
if(j) return true;

return false;

}
bool isexist( char *p){
vector<Position> find;
Position temp;
temp.p=g_State;
temp.i=0;
find.push_back(temp);
g_start=1;
g_finish=0;
g_maxi=-1;
bool canfinish=false;
while(!find.empty()){
if(temp.p->status&finish) {g_finish=temp.i;
break;
}
if(temp.i>g_maxi)
g_maxi=temp.i;
find.pop_back();
NextState(temp,p,find,temp.i);
if(!find.empty())
temp=find.back();
else if(g_maxi<(int)strlen(p)){
temp.i=g_maxi+1;
temp.p=g_State;
g_start=temp.i+1;//人是从1数而机器从0数,故加1
find.push_back(temp);
}
}
if(g_finish>=g_start) return true;
return false;
}
void output(char *p,int a,int b){
cout<<"\n位于"<<a<<"和"<<b<<"之间\n";
for(int i=a-1;i<b;i++)
cout<<p[i];
cout<<"\n";
}
void findWholeMatch(char *p){
char *str=p;
int rec=0;
while(isexist(p)&&rec<(int)strlen(str)){
output(str,g_start+rec,g_finish+rec);
rec+=g_finish;
p+=g_finish;
}

}
int main(){
char expression[100];
char content[1000];
fgets(expression,100,stdin);
fgets(content,1000,stdin);
construction(expression);
cout<<"构建完成 \n";
/*
if(isexist(content)){
cout<<"存在\n";
cout<<"开始在"<<g_start<<"结束在"<<g_finish<<"\n";
output(content,g_start,g_finish);
}
else cout<<"不存在\n";
*/

findWholeMatch(content);
system("pause");
realease();

}// regulatr.cpp : 定义控制台应用程序的入口点。
//

/*______________________________________________________
regular expression engine
written by 唐hz
2015.8.10
_______________________________________________________*/
#include "stdafx.h"
#include<iostream>
#include<vector>
using namespace std;
struct State;
#define error -1//此处不确定
#define normal 1
#define finish 16
#define mul_way 2
#define start 8
#define any 32
#define error_back 64
#define self_loop1 4
struct Linker{
State *from;
State *to;
char request;
};
struct LinkerList{
Linker l;
struct LinkerList *next;
};
struct State{
int status;
int position;
LinkerList * in;
LinkerList * out;
};
struct Position{
State* p;
int i;
};

State g_State[100];
int g_next[100];
int g_start,g_maxi=-1;
int g_can_not_any=-1;

void next(char *p){
int n=strlen(p);
int i=1,j=0;
g_next[j]=-1;
while(i<n){
if(p[i]==p[j])
g_next[i++]=j++;
else {
if(j!=0)
j=g_next[j-1]+1;
else{
j=0;
g_next[i++]=-1;
}
}

}
}

void addlinker(LinkerList **a,State *d){
if(*a==NULL){
(*a)=new LinkerList;
(*a)->l.request=b;
(*a)->l.from=c;
(*a)->l.to=d;
(*a)->next=NULL;
return ;
}
LinkerList * temp;
temp=new LinkerList;
temp->l.request=b;
temp->l.from=c;
temp->l.to=d;
temp->next=*a;
*a=temp;
return ;
}
void connect(State &a,&b);
}
}

void compile(char *p,j=0;
while(i<(int)strlen(q)){
if(q[i]=='|'){
i+=2;
}
else if(q[i]=='+'){
i++;
}
else p[j++]=q[i++];
}
p[j]='\0';
}

void construction(char *p){
char q[100];
compile(q,0);
while(*p!='\n')
{
switch(*p){
case '|':
if(i>0) i--;
p++;
g_State[i].status|=mul_way;
break;
case '+':
i--;
p--;
connect(g_State[i],1);
g_State[i].status|=normal;
i++;
p++;}
}
g_State[i].status|=finish;

}//查int 字节
void realease(){
LinkerList *temp,*temp1;
for(int i=0;i<100;i++)
{
temp=g_State[i].in;
while(temp){
temp1=temp;
temp=temp->next;
delete temp1;
}
temp=g_State[i].out;
while(temp){
temp1=temp;
temp=temp->next;
delete temp1;
}
}
}

bool NextState(Position a,int i){
auto temp=a.p->out;
Position rec;
bool j=false;
while(temp){
if(temp->l.request!=error&&temp->l.request==*(p+i)||(temp->l.request=='.'&&a.p->status&any&&a.p->position!=g_can_not_any)){
j=true;
if(temp->l.to->position==a.p->position&&temp->l.request!='.'&&a.p->status&any){
g_can_not_any=a.p->position;
}
rec.p=((temp->l).to);
rec.i=i+1;
if(rec.i<(int)strlen(p))
find.push_back(rec);
}
else
{
if((temp->l.request==error)&&(!(a.p->status&finish))&&!j){
if(((temp->l).to)->position<g_can_not_any){
cout<<"使g_can_not_any回复初值\n";
g_can_not_any=-1;}
rec.p=((temp->l).to);
rec.i=i;
if(!(rec.p->status&start))
find.push_back(rec);
}
}
temp=temp->next;
}
if(j) return true;

return false;

}
bool isexist( char *p){
vector<Position> find;
Position temp;
temp.p=g_State;
temp.i=0;
find.push_back(temp);
g_start=1;
g_finish=0;
g_maxi=-1;
bool canfinish=false;
while(!find.empty()){
if(temp.p->status&finish) {g_finish=temp.i;
break;
}
if(temp.i>g_maxi)
g_maxi=temp.i;
find.pop_back();
NextState(temp,g_finish+rec);
rec+=g_finish;
p+=g_finish;
}

}
int main(){
char expression[100];
char content[1000];
fgets(expression,g_finish);
}
else cout<<"不存在\n";
*/

findWholeMatch(content);
system("pause");
realease();

}

采用了NFA,未来将逐步更新。

较简单的正则匹配引擎实现的更多相关文章

  1. 用canvas做一个DVD待机动画的实现代码

    这篇文章主要介绍了用canvas做一个DVD待机动画的实现代码的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. HTML5自定义视频播放器源码

    这篇文章主要介绍了HTML5自定义视频播放器源码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  3. Html5 滚动穿透的方法

    这篇文章主要介绍了Html5 滚动穿透的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  4. HTML5自定义mp3播放器源码

    这篇文章主要介绍了HTML5自定义mp3播放器源码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  5. CSS中实现动画效果-附案例

    这篇文章主要介绍了 CSS中实现动画效果并附上案例代码及实现效果,就是CSS动画样式处理,动画声明需要使用@keyframes name,后面的name是人为定义的动画名称,下面我们来看看文章的具体实现内容吧,需要的小伙伴可以参考一下

  6. html5默认气泡修改的代码详解

    这篇文章主要介绍了html5默认气泡修改的代码详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  7. Html5移动端适配IphoneX等机型的方法

    这篇文章主要介绍了Html5移动端适配IphoneX等机型的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  8. 小程序瀑布流解决左右两边高度差距过大的问题

    这篇文章主要介绍了小程序瀑布流解决左右两边高度差距过大的问题的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. HTML5自定义元素播放焦点图动画的实现

    这篇文章主要介绍了HTML5自定义元素播放焦点图动画的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  10. CSS position属性和实例应用演示

    这篇文章主要介绍了CSS position属性和实例应用演示,absolute(绝对定位),relative(相对定位),relative与absolute的结合使用以及fixed(固定定位),需要的朋友可以参考下

随机推荐

  1. 法国电话号码的正则表达式

    我正在尝试实施一个正则表达式,允许我检查一个号码是否是一个有效的法国电话号码.一定是这样的:要么:这是我实施的但是错了……

  2. 正则表达式 – perl分裂奇怪的行为

    PSperl是5.18.0问题是量词*允许零空间,你必须使用,这意味着1或更多.请注意,F和O之间的空间正好为零.

  3. 正则表达式 – 正则表达式大于和小于

    我想匹配以下任何一个字符:或=或=.这个似乎不起作用:[/]试试这个:它匹配可选地后跟=,或者只是=自身.

  4. 如何使用正则表达式用空格替换字符之间的短划线

    我想用正则表达式替换出现在带空格的字母之间的短划线.例如,用abcd替换ab-cd以下匹配字符–字符序列,但也替换字符[即ab-cd导致d,而不是abcd,因为我希望]我如何适应以上只能取代–部分?

  5. 正则表达式 – /bb | [^ b] {2} /它是如何工作的?

    有人可以解释一下吗?我在t-shirt上看到了这个:它似乎在说:“成为或不成为”怎么样?我好像没找到’e’?

  6. 正则表达式 – 在Scala中验证电子邮件一行

    在我的代码中添加简单的电子邮件验证,我创建了以下函数:这将传递像bob@testmymail.com这样的电子邮件和bobtestmymail.com之类的失败邮件,但是带有空格字符的邮件会漏掉,就像bob@testmymail也会返回true.我可能在这里很傻……当我测试你的正则表达式并且它正在捕捉简单的电子邮件时,我检查了你的代码并看到你正在使用findFirstIn.我相信这是你的问题.findFirstIn将跳转所有空格,直到它匹配字符串中任何位置的某个序列.我相信在你的情况下,最好使用unapp

  7. 正则表达式对小字符串的暴力

    在测试小字符串时,使用正则表达式会带来性能上的好处,还是会强制它们更快?不会通过检查给定字符串的字符是否在指定范围内比使用正则表达式更快来强制它们吗?

  8. 正则表达式 – 为什么`stoutest`不是有效的正则表达式?

    isthedelimiter,thenthematch-only-onceruleof?PATTERN?

  9. 正则表达式 – 替换..与.在R

    我怎样才能替换..我尝试过类似的东西:但它并不像我希望的那样有效.尝试添加fixed=T.

  10. 正则表达式 – 如何在字符串中的特定位置添加字符?

    我正在使用记事本,并希望使用正则表达式替换在字符串中的特定位置插入一个字符.例如,在每行的第6位插入一个逗号是什么意思?如果要在第六个字符后添加字符,请使用搜索和更换从技术上讲,这将用MatchGroup1替换每行的前6个字符,后跟逗号.

返回
顶部