zoukankan      html  css  js  c++  java
  • 正则表达式匹配器------代码之美

        重要的、定义明确的、可扩展的基础正则表达式匹配器

    #include<iostream>
    using namespace std;
    //.(句点) 匹配任意的当个字符 
    //^ 匹配输入字符串的开头
    //$ 匹配输入字符串的结尾
    //* 匹配前一个字符的零个或者多个出现
    
    /*match: 在text中查找regexp*/
    int matchhere(char*regexp, char*text);
    int matchstar(int c, char *regexp, char *text);
    int match(char *regexp, char *text)
    {
    	if (regexp[0] == '^')
    		return matchhere(regexp + 1, text);
    	do {
    		if (matchhere(regexp, text))
    			return 1;
    	} while (*text++ != '');
    	return 0;
    }
    /*matchhere:在text的开头查找regexp*/
    int matchhere(char*regexp, char*text)
    {
    	if (regexp[0] == '')
    		return 1;
    	if (regexp[1] == '*')
    		return matchstar(regexp[0], regexp + 2, text);
    	if (regexp[0] == '$'&&regexp[1] == '')
    		return *text == '';
    	if (*text != '' && (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 != '' && (*text++ == c || c == '.'));
    	return 0;
    }
    /*matchstar:搜索c*regexp的出现最左以及最长的匹配*/
    int matchstar(int c, char *regexp, char*text,int n=0)
    {
    	char *t;
    	for (t = text; *t != '' && (*t == c || c == '.'); t++)
    		;
    	do {/*通配符*匹配零个或者多个实例*/
    		if (matchhere(regexp, t))
    			return 1;
    	} while (t-- > text);
    	return 0;
    }
    char a[100], b[100];
    int main() {
    	
    	while (cin >> a >> b)
    		cout << match(a, b) << endl;
    }
    

     

  • 相关阅读:
    软件工程结对编程作业
    软件工程第1次作业
    阅读一篇文章,培养一个习惯
    OpenvSwitch系列之五 网桥特性功能配置
    读《阿里工程师的自我修养》我学到这几点
    OpenvSwitch系列之四 ovs-ofctl命令使用
    OpenvSwitch系列之三 ovs-vsctl命令使用
    python进阶之垃圾回收
    OpenDaylight开发hello-world项目之功能实现
    python进阶之内存模型
  • 原文地址:https://www.cnblogs.com/l2017/p/9304382.html
Copyright © 2011-2022 走看看