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 ...
随机推荐
- 流畅的python第七章函数装饰器和闭包学习记录
本章讨论的话题 python如何计算装饰器句法 python如何判断变量是不是局部的(通过函数内部是否给变量赋值过来判断是否是局部变量) 闭包存在的原因和工作原理(闭包是一种函数,它会保留定义函数时存 ...
- 一个故事讲清NIO
假设某银行只有10个职员.该银行的业务流程分为以下4个步骤: 1) 顾客填申请表(5分钟): 2) 职员审核(1分钟): 3) 职员叫保安去金库取钱(3分钟): 4) 职员打印票据,并将钱和票据返回给 ...
- windows下创建vp9的VS版本
1. webm官网 下载版本: http://code.google.com/p/webm/downloads/list 创建过程这里有比较详细的英文说明: http://www.webmproj ...
- web 表单,脚本验证
1.不能含有中文 var obj = document.form1.txtName.value; if(/.*[\u4e00-\u9fa5]+.*$/.test(obj)) { alert(" ...
- Android colors.xml 颜色列表
android 常用项 <?xml version="1.0" encoding="utf-8"?> <resources> <c ...
- iOS设备定位服务开启判定
应用CLLocationManager 的两个方法 [CLLocationManagerlocationServicesEnabled] 判断设备是否开启定位功能 [CLLocationManager ...
- vue - webpack.dev.conf.js for FriendlyErrorsPlugin
描述:webpack网页端友好的报错信息就来自它 官网:https://www.npmjs.com/package/friendly-errors-webpack-plugin new Friendl ...
- VS报表图解《一》---菜鸟版
与原先的开发环境VB.EXE不同VS2013自带了报表控件ReportViewer能够内部实现报表的设计,本文主要通过绑定数据集来实现报表的显示 1.加入:reportviewer控件,当将控件显示在 ...
- java设计模式之策略
今天你的leader兴致冲冲地找到你,希望你可以帮他一个小忙,他现在急着要去开会.要帮什么忙呢?你很好奇. 他对你说,当前你们项目的数据库中有一张用户信息表,里面存放了很用户的数据,现在需要完成一个选 ...
- 乐鑫esp8266的串口通讯驱动源文件,nonos和rtos版本
目录 一.前言: 二.esp8266的串口分布情况: 三.esp8266的串口通讯时候,应该怎么接线: 四.esp8266的NONOS非系统,串口编程: 五.esp8266的RTOS实时系统,串口编程 ...