Linux中自带正则表达式应用举例

 环境:Fedora12, C程序:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>// 提取子串
char* getsubstr(char *s, regmatch_t *pmatch)
{static char buf[100] = {0};memset(buf, 0, sizeof(buf));memcpy(buf, s+pmatch->rm_so, pmatch->rm_eo - pmatch->rm_so);return buf;
}int main(int argc, char **argv)
{int status, i;int cflags = REG_EXTENDED;regmatch_t pmatch[5];const size_t nmatch = 5;regex_t reg;const char *pattern = "([A-Z]+)([a-z]+)ID[0-9]+@([a-z]+)\\.([a-z]+)";	// 正则表达式char buf[] = "COMEdavID2012@gmail.com";		// 待搜索的字符串regcomp(®, pattern, cflags);status = regexec(®, buf, nmatch, pmatch, 0);if(status == REG_NOMATCH)printf("No Match\n");else{printf("Match:\n");for(i = 0; i < nmatch; i++){if(pmatch[i].rm_so == -1)continue;char *p = getsubstr(buf, &pmatch[i]);printf("[%d, %d): %s\n", pmatch[i].rm_so, pmatch[i].rm_eo, p);}}regfree(®);return 0;
}


编译运行:

[zcm@t #52]$make
gcc    -c -o a.o a.c
gcc  -o a a.o
[zcm@t #53]$./a
Match:
[0, 23): COMEdavID2012@gmail.com
[0, 4): COME
[4, 7): dav
[14, 19): gmail
[20, 23): com
[zcm@t #54]$


注意

pmatch[0]用来匹配整个正则表达式

pmatch[1]用来匹配子模式1

pmatch[2]用来匹配子模式2

......

 

思考

所以如果想从待搜索的字符串中搜索出所有匹配的结果(假设大于1个),怎么办呢?------- 循环调用regexec,代码如下:

/*Posix正则表达式应用:循环调用regexec(),以获得多个匹配的结果
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>// 提取子串
char* getsubstr(char *s, regmatch_t *pmatch)
{static char buf[100] = {0};memset(buf, 0, sizeof(buf));memcpy(buf, s+pmatch->rm_so, pmatch->rm_eo - pmatch->rm_so);return buf;
}int main(int argc, char **argv)
{int status, i;int cflags = REG_EXTENDED;regmatch_t pmatch[10];const size_t nmatch = 10;regex_t reg;//const char *pattern = "([A-Z]+)([a-z]+)(ID|DB)[0-9]+@([a-z]+)\\.([a-z]+)";	// 正则表达式const char *pattern = "[[:upper:]]+([[:lower:]]+)";	// 正则表达式char buf[] = "c COMEdavDB2012@gmail.com ZHOUcimingID2030@sohu.com";		// 待搜索的字符串char *pSrc = buf, *p = NULL;int next = 0;int mCount = 1;											// 匹配的次数int len = strlen(buf);regcomp(®, pattern, cflags);							// 编译正则表达式do														// 循环搜索匹配的结果{printf("pSrc = %s\n", pSrc);status = regexec(®, pSrc, nmatch, pmatch, 0);if(status == REG_NOMATCH)							// 未找到匹配的结果{printf("No Match%d\n", mCount);break;}else{printf("Match%d:\n", mCount);for(i = 0; i < nmatch; i++)						// 输出此次匹配的结果(包括子模式){if(pmatch[i].rm_so == -1)break;p = getsubstr(pSrc, &pmatch[i]);printf("pmatch[%d] = [%d, %d): %s\n", i, pmatch[i].rm_so, pmatch[i].rm_eo, p);}putchar('\n');pSrc = pSrc + pmatch[0].rm_eo;					// 后移搜索的起始位置}mCount++;}while(pSrc < buf + len - 1);regfree(®);return 0;
}
编译运行:

[zcm@t #157]$make
gcc    -c -o a2.o a2.c
gcc  -o a2 a2.o
[zcm@t #158]$./a2
pSrc = c COMEdavDB2012@gmail.com ZHOUcimingID2030@sohu.com
Match1:
pmatch[0] = [2, 9): COMEdav
pmatch[1] = [6, 9): davpSrc = DB2012@gmail.com ZHOUcimingID2030@sohu.com
Match2:
pmatch[0] = [17, 27): ZHOUciming
pmatch[1] = [21, 27): cimingpSrc = ID2030@sohu.com
No Match3
[zcm@t #159]$



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/406575.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ISAPI_Rewrite 规则说明

I (ignore case&#xff09;不管大小写强行指定字符匹配例&#xff1a;RewriteRule /code/project/([0-9,a-z]*).html /soft.jsp\?softpy$1 [I]其他的参数一览I (ignore case&#xff09;不管大小写强行指定字符匹配&#xff0c;这个FLAG影响RewriteRule指令和相应的RewriteCo…

H5页面唤起指定app或跳转到应用市场

场景1&#xff1a; 在 h5 页面上&#xff0c;不管用户是否安装过该app&#xff0c;都直接跳转到应用市场&#xff0c;让用户从应用市场上打开app。 思路&#xff1a; 这种场景处理比较简单&#xff0c;直接判断判断是android端还是ios端&#xff0c;然后在点击按钮上赋值对应…

MyBatis.Net 学习手记

MyBatis.NET的前身为IBatis&#xff0c;是JAVA版MyBatis在.NET平台上的翻版&#xff0c;相对NHibernate、EntityFramework等重量级ORM框架而言&#xff0c;MyBatis.NET必须由开发人员手动写SQL&#xff0c;相对灵活性更大&#xff0c;更容易保证DB访问的性能&#xff0c;适用开…

Python 使用 UTF-8 编码

From: http://blog.chenlb.com/2010/01/python-use-utf-8.html 一般我喜欢用 utf-8 编码&#xff0c;在 python 怎么使用呢&#xff1f; 1、在 python 源码文件中用 utf-8 文字。一般会报错&#xff0c;如下&#xff1a; File "F:\workspace\psh\src\test.py", line …

curl下载文件的命令

curl文件下载 curl将下载文件输出到stdout&#xff0c;将进度信息输出到stderr&#xff0c;不显示进度信息使用–silent 选项。1 . curl URL --silent 这条命令是将下载文件输出到终端&#xff0c;所有下载的数据都被写入到stdout。2 . curl URL --silent -O 使用选项 -O 将下载…

后台运行python程序 遇到缓冲区问题

From: http://www.iteye.com/topic/867446 环境&#xff1a;linux 一段执行时间很长的程序&#xff08;用python做hive客户端执行mapreduce&#xff09; 在linux后台执行&#xff0c;把结果输出到某文件&#xff1a; Python代码 python xxx.py > log.log& 遇到的问题…

[nodejs][html5][css3][js] 个人网站上线

各个功能详细代码 http://www.cnblogs.com/wangxinsheng/p/4263591.html 2015年1月31日 --- 虽然比较懒&#xff0c;但终于匆忙的弄了个个人网站上线&#xff0c;没有博客功能。。。只有些数据抓取&#xff0c;百度地图&#xff0c;视屏游戏功能。 可是heroku站点在国内的速度超…

各种URL生成方式的性能对比

在上一篇文章中我们列举了各种URL生成的方式&#xff0c;其中大致可以分为三类&#xff1a; 直接拼接字符串&#xff08;方法一及方法二&#xff09; 使用Route规则生成URL&#xff08;方法三&#xff09; 使用Lambda表达式生成URL&#xff08;方法四及方法五&#xff09; 我们…

element-ui中el-table的表头、内容样式

方式1&#xff1a; 直接在标签上添加上属性值&#xff1a; <el-table:header-cell-style"{background:#F3F4F7,color:#555}" ></el-table>方式2&#xff1a; 在method里面写上方法&#xff1a; rowClass({ row, rowIndex}) {console.log(rowIndex) //表…

python下设置urllib连接超时

From: http://blog.csdn.net/vah101/article/details/6175406 首先导入socket库 import socket 在开始连接前的代码中&#xff0c;再加入 socket.setdefaulttimeout(6) #6秒内没有打开web页面&#xff0c;就算超时 然后就可以开始连接了&#xff0c;比如 try: …

请移步到我的新浪博客

请移步到我的新浪博客http://blog.sina.com.cn/highlandcat转载于:https://blog.51cto.com/highlandcata/221449

疯狂喷气机

2/3D游戏&#xff1a;2D 辅助插件&#xff1a;原生 游戏制作难度系数&#xff1a;初级 游戏教程网址&#xff1a;http://www.raywenderlich.com/69392/make-game-like-jetpack-joyride-unity-2d-part-1 1、控制摄像机跟随人物移动 public GameObject targetObject; //目标对象p…

elementui表格-改变某一列的样式

cellStyle({ row, column, rowIndex, columnIndex }) {if (columnIndex 0) {// 指定列号return ‘padding:0‘} else {return ‘‘} },

正则表达式基础(一)

From: http://www.usidcbbs.com/read-htm-tid-1457.html Perl 中的正则表达式 正则表达式是 Perl 语言的一大特色&#xff0c;也是 Perl 程序中的一点难点&#xff0c;不过如果大家能够很好的掌握他&#xff0c;就可以轻易地用正则表达式来完成字符串处理的任务&#xff0…

CodeSmith--SchemaExplorer类结构详细介绍

CodeSmith----SchemaExplorer类结构详细介绍 CodeSmith与数据库的联系&#xff0c;在CodeSmith中自带一个程序集SchemaExplorer.dll&#xff0c;这个程序集中的类主要用于获取数据库中各种对象的结构。 <% Property Name"SourceTable" Type"SchemaExplorer.T…

vue element项目常见实现表格内部可编辑功能

目录 前言 正文 1.简单表格行内内部可编辑 2. 数据从后端取得表格行内可编辑 3.批量表格整体的可编辑 结语 前言 后台系统都是各种表格表单编辑&#xff0c;整理了下常见的几种实现表格编辑的方式&#xff0c;希望有用。使用框架&#xff1a;vueelement 表格行内内部可编辑 数…