正则表达式匹配算法
来源:互联网 发布:网络中了500万怎么领奖 编辑:程序博客网 时间:2024/03/29 22:57
看《代码之美》之美中有个简短而高效的正则表达式匹配算法,这里给一下简单的实现,供学习使用。
#include <iostream>#include<String>#include<stdio.h>using namespace std;int match(char * regexp, char * text);int matchhere(char * regexp, char * text);int matchstar(int c, char *regexp, char * text);/* match: 在text中查找regexp */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;}/* matchhere: 在text中的开头查找regexp */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[0] == '\0'){ return *text == '\0'; } if(*text != '\0' && (regexp[0] == '.' || regexp[0] == *text)){ return matchhere(regexp + 1, text + 1); } return 0;}/* matchstar: 在text的开头查找C*regexp */int matchstar(int c, char *regexp, char * text){ do{/* 通配符*匹配零个或者多个实例*/ if(matchhere(regexp, text)){ return 1; } }while(*text != '\0' && (*text++ == c || c == '.')); return 0;}int main(){ char * StrSource = "Plastic We use plastic wrap to protect our foods. We put our garbage in plastic bags or plastic cans. We sit on plastic chairs, play with plastic toys, drink from plastic cups, and wash our hair with shampoo from plastic bottles!Plastic does not grow in nature. It is made by mixing certain things together. We call it a produced or manufactured material. Plastic was first made in the 1860s from plants, such as wood and cotton. That plastic was soft and burned easily."; char mystr[50]; while(true){ scanf("%s",mystr); if(match(mystr, StrSource)){ cout<<"匹配成功!"<<endl; }else{ cout<<"匹配失败!"<<endl; } } return 0;}
0 0
- 正则表达式匹配算法
- 【算法】正则表达式匹配
- 正则表达式匹配加减法算法
- 算法#20--正则表达式匹配原理
- 详解正则表达式匹配算法原理
- 正则表达式, NFA, DFA, AC多模匹配算法
- 正则表达式字符匹配
- 正则表达式 匹配"<"方法!
- 正则表达式匹配规则
- 正则表达式匹配规则
- 正则表达式最小匹配
- 正则表达式匹配字符串
- 正则表达式匹配
- 正则表达式匹配汉字
- 正则表达式匹配规则
- 正则表达式匹配关键字
- 正则表达式多次匹配
- 正则表达式匹配
- linux下查看网络端口占用情况
- JPA 持久化规范----简单应用
- JavaScript打开窗口函数(Window.Open)使用详解
- TIME_WAIT
- 基于SQL Server 2008事物复制的多到一同步
- 正则表达式匹配算法
- 正则表达式---获取英文双引号里面的内容
- 国外 2D 引擎相关站点收集
- 网站建设优化如何做好内部页面的优化
- 解决错误:/gen already exists but is not a source folder. Convert to a source folder or rename it
- 开始iOS 7中自动布局教程(一)
- enable_share_from_this功能介绍
- JavaScript权威指南学习笔记——JavaScript概述
- 字符串加密处理