语法

       #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 []:[-:-]

明显不是想要的结果,需继续测试。

最新文章

  1. Python系统命令操作
  2. 20145227《Java程序设计》第10周学习总结
  3. linux 修改时间 - [命令操作]
  4. Delphi word 颜色
  5. 【hdu2896】病毒侵袭
  6. MSSQL数字时间(timestamp)转换为DATETIME
  7. linux远程管理工具
  8. webview加载网页加载不出来
  9. Git@OSC &amp; SSH配置
  10. OGG-02803 Encountered a Data Guard role transition
  11. 04 Tensorflow的中的常量、变量和数据类型
  12. linux上安装完torch后仍报错:ImportError: No module named torch
  13. C# Winfom 中ListBox的简单用法
  14. ElasticSearch - Shard数调优(ElasticSearch性能)
  15. windows程序设计 加载位图图片
  16. Java 二进制数据转成文件
  17. SqlServer语句
  18. MVC 5 Strongly Typed Views(强类型视图)
  19. 如果想使用GIT Extentions的解决冲突窗口,安装时必须勾选KDIFF3
  20. python 读取单所有json数据写入mongodb(单个)

热门文章

  1. Gpu driven rendering pipelines &amp; bindless texture
  2. 关于C#中async/await中的异常处理(上)
  3. IIS 服务器隐藏index.php 的方法
  4. 【Web】Javascript、Python、Django模板配合处理URL Encode
  5. 【Web】前台传送JSON格式数据到后台Shell处理
  6. 十大迷你iPhone天气应用
  7. iOS12适配指南
  8. JS动画公式
  9. Cannot make a static reference to the non-static method的解决方法
  10. servlet 简单filter避免中文乱码等