歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> LinuxC支持正則表達式的字符串替換函數(一)

LinuxC支持正則表達式的字符串替換函數(一)

日期:2017/2/25 10:38:17   编辑:Linux教程
 /**

  * Linux C 支持正則表達式的字符串替換函數

  *

  *

  */

  #include <stdio.h>

  #include <stdlib.h>

  #include <string.h>

  #include <sys/types.h>

  #include <unistd.h>

  //regex

  #include <regex.h>

  //cns_reg函數的返回類型

  typedef struct _reg_rtn_struct

  {

  int rtn; //成功與否標志0 成功, 1失敗

  int pstart; //匹配到的子串開始位移

  int pend; //匹配到的子串尾部位移

  } reg_rtn_struct;

  /**

  *

  * 正則表達式查找函數

  */

  reg_rtn_struct cns_reg(const char *str, const char *pattern)

  {

  reg_rtn_struct reg_rtn_struct_var;

  int z; //status

  int pos; //配置處的位置

  int cflags = REG_EXTENDED; //compile flags

  regex_t reg; //compiled regular expression

  char ebuf[128]; //error buffer

  bzero(ebuf, sizeof(ebuf));

  regmatch_t pm[10]; //pattern matches 0-9

  bzero(pm, sizeof(pm));

  const size_t nmatch = 10; //The size of array pm[]

  //編譯正則表達式

  /**

  *

  * @param const char* pattern 將要被編譯的正則表達式

  * @param regex_t* reg 用來保存編譯結果

  * @param int cflags 決定正則表達式將如何被處理的細節

  *

  * @return success int 0 並把編譯結果填充到reg結構中

  * fail int 非0

  *

  */

Copyright © Linux教程網 All Rights Reserved