语法

       #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(&reg, "Chin.", REG_NEWLINE | REG_EXTENDED);
if(ret != ){
perror("regcomp");
exit();
} ret = regexec(&reg, 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(&reg); 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的更多相关文章

  1. C语言正则表达式详解 regcomp() regexec() regfree()详解

    标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库 ...

  2. linnux 3

    kill [信号代码] 进程ID 以优雅的方式结束进程# kill -l PID-l选项告诉kill命令用好像启动进程的用户已注销的方式结束进程.当使用该选项时,kill命令也试图杀死所留下的子进程. ...

  3. 笔记整理——Linux下C语言正则表达式

    Linux下C语言正则表达式使用详解 - Google Chrome (2013/5/2 16:40:37) Linux下C语言正则表达式使用详解 2012年6月6日Neal627 views发表评论 ...

  4. 从零开始攻略PHP(5)——字符串操作与POSIX正则

    一.字符串操作 1.字符串的格式化 1.1 干掉空格 trim()函数可以除去字符串开始位置和结束位置的空格,并将结果字符串返回. ltrim()函数可以除去字符串开始位置的空格. rtrim()函数 ...

  5. The POSIX API/nss/nscd

    https://code.google.com/p/nsscache/wiki/BackgroundOnNameServiceSwitch The POSIX API POSIX is a stand ...

  6. 串口通信编程向导 Serial Programming Guide for POSIX Operating Systems

    https://www.cmrr.umn.edu/~strupp/serial.html#CONTENTS Introduction Chapter 1, Basics of Serial Commu ...

  7. PHP学习(5)——字符串操作与POSIX正则

    一.字符串操作 1.字符串的格式化 1.1 干掉空格 trim()函数可以除去字符串开始位置和结束位置的空格,并将结果字符串返回. ltrim()函数可以除去字符串开始位置的空格. rtrim()函数 ...

  8. centos 7.0 phpize 扩展php

    phpize扩展php模块 phpize 所在目录 /usr/etc/php/bin/phpize 查看当前php配置情况 /usr/etc/php/bin/下面的php [root@localhos ...

  9. 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 ...

随机推荐

  1. Jpeglib读取jpg文件 【转】

    http://blog.csdn.net/blues1021/article/details/45424695 整理自 : http://hi.baidu.com/lewutian/item/e8ee ...

  2. 手写Json转换

    在做项目的时候总是要手动将集合转换成json每次都很麻烦,于是就尝试着写了一个公用的方法,用于转换List to json: using System; using System.Collection ...

  3. java 注解(自身理解)

    声明注解 使用注解 解析注解 产生的结果 注解利用的是反射机制 ============================================================= 使用注解修饰 ...

  4. PHP实现程序单例执行

    原创文章,转载请注明出处:http://huyanping.sinaapp.com/?p=222 作者:Jenner 一.场景描写叙述: 近期我们一块业务.须要不断的监听一个文件夹的变化.假设文件夹中 ...

  5. swift 使用匿名函数初始化属性

    swift 使用匿名函数初始化属性 匿名函数格式: { //代码体 }() 大括号是定义函数体的,小括号()是调用函数体的. 为什么不写一个函数然后调用函数呢?为何用匿名函数呢? 主要还是代码的简洁性 ...

  6. iframe中,页面转换后回到页面的顶部

    看到网上有这样描述的: 现在A页面内分为上下两个部分,上部分是top,下部分分左右,左是treeview右边是iframe,iframe内嵌一个B页面,B页面的内容实质上是个月刊,可以理解为杂志,里面 ...

  7. Jquery滑动门实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Php如何返回json数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  9. Vue 表单

    1.v-model v-model不再关心初始化的value值. <!DOCTYPE html> <html lang="zh"> <head> ...

  10. 纯css3响应式3d翻转菜单

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 周末快乐哈,今天来看一个纯CSS3实现的3d翻转菜单.3d响应式菜单,希望对大家有所帮助. 在线赞赏效果.在线编辑代码,或者下载收藏. ...