语法

       #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. FXC Define的使用方法

    https://docs.microsoft.com/en-us/windows/desktop/direct3dtools/dx-graphics-tools-fxc-syntax https:// ...

  2. SQL Server-数据库中强varchar类型使用sum函数计算总和

    select sum(cast(totalmoney AS DECIMAL)) as totalmoney from dbo.t_wxbill

  3. 教你用 google-drive-ocamlfuse 在 Linux 上挂载 Google Drive

    如果你在找一个方便的方式在 Linux 机器上挂载你的 Google Drive 文件夹, Jack Wallen 将教你怎么使用 google-drive-ocamlfuse 来挂载 Google ...

  4. 【笔记】探索js 的this 对象 (第一部分)

    最近在看 你不知道的javascript 这本书,在第二部分看到了一个比较重要的知识点 那就是 this对象的全面认识,于是做一下笔记 博主本人在看这本书之前也一直以为 this 是指一切引用类型的本 ...

  5. DotNetBar ComboBoxEx

    DotNetBar ComboBoxEx using System; using System.ComponentModel; using System.Drawing; using System.R ...

  6. java中的Iterator接口

    Iterator接口 Iterator接口也是Java集合框架的成员,但它与Collection系列.Map系列的集合不一样:Collection系列集合.Map系列集合主要用于盛装其他对象,而Ite ...

  7. django+nginx+uwsgi 项目部署

    Django虽然自带一个Server,但只能作为开发时测试使用,我们需要一个可以稳定而持续的服务器对网站进行部署,比如Apache, Nginx, lighttpd等,本篇将利用nginx和uWSGI ...

  8. Linux下SVN部署/安全及权限配置,实现web同步更新

    转自:http://www.cnblogs.com/me115/archive/2013/04/07/3002058.html 本文包含以下内容: SVN服务器安装 SVN权限管理 SVN使用SASL ...

  9. LR11.50 通过Mobile Application 录制手机操作

    LR11.50 通过Mobile Application 录制手机操作 步骤就是 1:新建LR脚本.协议选择Mobile Application - HTTP/HTML 2:在record里选择第三个 ...

  10. html+css+js实现科学计算器

    代码地址如下:http://www.demodashi.com/demo/13751.html 项目描述 纯html+css+js实现一个科学计算器,支持平方开方指数对数等基本函数,支持键盘输入,有简 ...