regcomp/regexec/regfree--POSIX regex functions
语法
#include <sys/types.h>
#include <regex.h> int regcomp(regex_t *preg, const char *regex, int cflags); int regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags); size_t regerror(int errcode, const regex_t *preg, char *errbuf,
size_t errbuf_size); void regfree(regex_t *preg);
解析
正则表达式库函数主要分两部分,正则表达式编译和匹配。编译用于正则表达式提供格式(程序识别),匹配用于提供匹配位置便于提取。
regcomp() is used to compile a regular expression into a form that is suitable for subsequent regexec() searches.
regexec() is used to match a null-terminated string against the precompiled pattern buffer, preg. nmatch and pmatch are used to provide information regarding the location of any matches. eflags may be the bitwise-or of one or both of REG_NOTBOL and REG_NOTEOL which cause changes in matching behavior described below.
示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h> #define MATCH_NUM 3 char str[]={"Chinese love China! China, fighting!\n"}; int main(int argc, char **argv)
{
regex_t reg;
int ret = , i = ;
regmatch_t matches[MATCH_NUM];
char *ps = NULL; ret = regcomp(®, "Chin.", REG_NEWLINE | REG_EXTENDED);
if(ret != ){
perror("regcomp");
exit();
} ret = regexec(®, str, MATCH_NUM, matches, );
if(ret != ){
perror("regexec");
exit();
}
for(i = ; i < MATCH_NUM; i++){
ps = strndup(str + matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
if(ps)
printf("The match %dst is [%s]:[%d:%d]\n", i, ps, matches[i].rm_so, matches[i].rm_eo);
free(ps);
} regfree(®); return ;
}
~$gcc regex.c -Wall
~$./a.out
The match 0st is [Chine]:[:]
The match 1st is []:[-:-]
The match 2st is []:[-:-]
明显不是想要的结果,需继续测试。
regcomp/regexec/regfree--POSIX regex functions的更多相关文章
- C语言正则表达式详解 regcomp() regexec() regfree()详解
标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库 ...
- linnux 3
kill [信号代码] 进程ID 以优雅的方式结束进程# kill -l PID-l选项告诉kill命令用好像启动进程的用户已注销的方式结束进程.当使用该选项时,kill命令也试图杀死所留下的子进程. ...
- 笔记整理——Linux下C语言正则表达式
Linux下C语言正则表达式使用详解 - Google Chrome (2013/5/2 16:40:37) Linux下C语言正则表达式使用详解 2012年6月6日Neal627 views发表评论 ...
- 从零开始攻略PHP(5)——字符串操作与POSIX正则
一.字符串操作 1.字符串的格式化 1.1 干掉空格 trim()函数可以除去字符串开始位置和结束位置的空格,并将结果字符串返回. ltrim()函数可以除去字符串开始位置的空格. rtrim()函数 ...
- The POSIX API/nss/nscd
https://code.google.com/p/nsscache/wiki/BackgroundOnNameServiceSwitch The POSIX API POSIX is a stand ...
- 串口通信编程向导 Serial Programming Guide for POSIX Operating Systems
https://www.cmrr.umn.edu/~strupp/serial.html#CONTENTS Introduction Chapter 1, Basics of Serial Commu ...
- PHP学习(5)——字符串操作与POSIX正则
一.字符串操作 1.字符串的格式化 1.1 干掉空格 trim()函数可以除去字符串开始位置和结束位置的空格,并将结果字符串返回. ltrim()函数可以除去字符串开始位置的空格. rtrim()函数 ...
- centos 7.0 phpize 扩展php
phpize扩展php模块 phpize 所在目录 /usr/etc/php/bin/phpize 查看当前php配置情况 /usr/etc/php/bin/下面的php [root@localhos ...
- centos 7.0 PHP 5.6.5 安装过程 (php+nginx)
php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...
随机推荐
- Dev之ribbon设置
- Shadow Map 原理和改进 【转】
http://blog.csdn.net/ronintao/article/details/51649664 参考 1.Common Techniques to Improve Shadow Dept ...
- unity mipmap 糊
unity 开miapmap会糊很多 尤其在editor里面 我估计和editor的 tempRT resolution 957x380有关 -----确实是这样 手机上糊的程度低很多 中间rt我用的 ...
- 判断一组checkbox中是否有被选中的
if ($(":checkbox[name=subcheck]:checked").size() == 0) { alert("请至少选择一条记录进行删除操作!" ...
- [转载]抓包工具Charles乱码解决办法
FROM: http://blog.csdn.net/huanghanqian/article/details/52973651 安装 SSL 证书 Mac 端 首先去 http://www.char ...
- Linux环境搭建:2. 安装Ubuntu虚拟机
我家淘宝店,主要协助同学做毕业设计:https://shop104550034.taobao.com/?spm=2013.1.1000126.d21.pPCzDZ 1. 创建虚拟机 1. 打开VMwa ...
- chrome浏览器 提示Adobe Flash Player未安装的解决方法
最近遇到了个flash player设置的一个问题,记录一下,可能不同浏览器版本和设置不一样 浏览器版本:版本 61.0.3163.100(正式版本) (64 位) 打开需要flash player的 ...
- List分组迭代器 C#--深入理解类型
List分组迭代器 说明: 针对长度较大的List对象,可以分组批量进行处理, 如:长度为1000的List对象,可分为10组,每组100条,对数据进行业务逻辑处理... Source /**** ...
- VS2012安装git
新安装了vs2012,尝试使用Git,安装过程比较简单,记录下来以备以后查阅. 1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http ...
- public_brokers
This page is an effort to list the publically-accessible MQTT brokers, often useful for testing and ...