C语言正则表达式问题

lyw506017026 2015-07-03 02:36:42
最近刚接触C语言的正则表达式

写了个程序
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

typedef struct _reg_rtn_struct { //保存正则表达式匹配结果
int rtn; //成功与否标志0 成功, 1失败
int pstart; //匹配到的子串开始位移
int pend; //匹配到的子串尾部位移
} REGRTNSTRUCT;

static int cnsreg(const char *str, const char *pattern, REGRTNSTRUCT reg_rtn_struct_var[], const int structcount ){

int i;
int z; //函数返回值
int cflags = REG_EXTENDED; //cflags的取值范围为:REG_EXTENDED:扩展规则表达式的方式进行匹配
// REG_NEWLINE:识别换行符
// REG_ICASE:匹配字母时忽略大小写
regex_t reg; //用来保存编译后的正则表达式的regex_t结构体指针
char ebuf[128] = {0}; //保存错误信息
regmatch_t pm[structcount]; //存放匹配文本串在目标串中的开始、结束位置的结构数组
const size_t nmatch = structcount; //表示regmatch_t结构体数组的长度

//编译正则表达式
//GATHERRULEDEBUG("cnsreg str = %s, pattern = %s\n", str, pattern);
z = regcomp(®, (const char*)pattern, cflags);
if ( z != 0)
{
regerror(z, ®, ebuf, sizeof(ebuf));
//GATHERRULEDEBUG("cnsreg_error %s: pattern '%s'\n", ebuf, pattern);
regfree(®);
return 1;
}

z = regexec(®, str, nmatch, pm, 0);
//没有找到匹配数据
if (z == REG_NOMATCH)
{
regfree(®);
return 2;
}
else if ( z != 0 )
{
regerror(z, ®, ebuf, sizeof(ebuf));
//GATHERRULEDEBUG("cnsreg_error %s: regcomp('%s')\n", ebuf, str);
regfree(®);
return 3;
}

/*保存匹配的位置*/
for ( i = 0; i < structcount; i++ )
{
if (pm[i].rm_so != -1)
{
reg_rtn_struct_var[i].rtn = 0;
reg_rtn_struct_var[i].pstart = pm[i].rm_so;
reg_rtn_struct_var[i].pend = pm[i].rm_eo;
}
else
{
reg_rtn_struct_var[i].rtn = 1;
reg_rtn_struct_var[i].pstart = 0;
reg_rtn_struct_var[i].pend = 0;
}
}
regfree(®);
return 0;
}


static char* substr(const char*str, unsigned start, unsigned end)
{
unsigned n = end - start;
static char stbuf[256];
strncpy(stbuf, str + start, n);
stbuf[n] = 0;
return stbuf;
}



int main(int argc, char** argv){
char para_match[1024]; //临时存储匹配规则
REGRTNSTRUCT reg_tmp[1024];
int lastseq, thisseq, minseq, maxseq;
int r = cnsreg(argv[1],argv[2],reg_tmp,1024);
int i=0;
for(i=0;i<1024;i++){
if(reg_tmp[i].rtn==0){
lastseq = atoi(substr(argv[1],reg_tmp[i].pstart,reg_tmp[i].pend));
printf("%d",lastseq);
}
}


求高人指点下 我要截取 abc20150703551111.012txt 这个字符串 并把子串 55 提取出来 要怎么写这个正则表达式
试了好久,都没成功、
...全文
216 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-07-06
  • 打赏
  • 举报
回复
仅供参考:
//摘自《代码之美》
// 字符     含义
// .        匹配任意的单个字符
// ^        匹配输入字符串的开头
// $        匹配输入字符串的结尾
// *        匹配前一个字符的零个或者多个出现
#include <stdio.h>
int matchhere(char *regexp, char *text);

int matchstar(int c, char *regexp, char *text) {// matchstar: search for c*regexp at beginning of text
   do {// a * matches zero or more instances
       if (matchhere(regexp, text)) return 1;
   } while (*text != '\0' && (*text++ == c || c == '.'));
   return 0;
}
int matchhere(char *regexp, char *text) {// matchhere: search for regexp at beginning of text
   if (regexp[0] == '\0') return 1;
   if (regexp[1] == '*') return matchstar(regexp[0], regexp+2, text);
   if (regexp[0] == '$' && regexp[1] == '\0') return *text == '\0';
   if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) return matchhere(regexp+1, text+1);
   return 0;
}

int match(char *regexp, char *text) {// match: search for regexp anywhere in text
    if (regexp[0] == '^') return matchhere(regexp+1, text);
    do {// must look even if string is empty
        if (matchhere(regexp, text)) return 1;
    } while (*text++ != '\0');
    return 0;
}
void main() {
    printf("%d==match(abc ,abc)\n",match("abc" ,"abc"));
    printf("%d==match(^a  ,abc)\n",match("^a"  ,"abc"));
    printf("%d==match(c$  ,abc)\n",match("c$"  ,"abc"));
    printf("%d==match(a.c ,abc)\n",match("a.c" ,"abc"));
    printf("%d==match(a.*c,abc)\n",match("a.*c","abc"));
    printf("-------------------\n");
    printf("%d==match(ABC ,abc)\n",match("ABC" ,"abc"));
    printf("%d==match(^B  ,abc)\n",match("^B"  ,"abc"));
    printf("%d==match(A$  ,abc)\n",match("A$"  ,"abc"));
    printf("%d==match(a..c,abc)\n",match("a..c","abc"));
    printf("%d==match(a.*d,abc)\n",match("a.*d","abc"));
}
//1==match(abc ,abc)
//1==match(^a  ,abc)
//1==match(c$  ,abc)
//1==match(a.c ,abc)
//1==match(a.*c,abc)
//-------------------
//0==match(ABC ,abc)
//0==match(^B  ,abc)
//0==match(A$  ,abc)
//0==match(a..c,abc)
//0==match(a.*d,abc)
super_admi 2015-07-04
  • 打赏
  • 举报
回复
我靠,楼主是想自己写一个正则库?
lniwn 2015-07-03
  • 打赏
  • 举报
回复
如果位数固定了直接取就行了,这样速度最快,如果位数不固定,用正则的话楼上已经写了。如果不能满足要求,再将后面的加上
fly_dragon_fly 2015-07-03
  • 打赏
  • 举报
回复
看起来用不上正则, 试试吧 [a-z]+\\d{8}(\\d\\d)
lyw506017026 2015-07-03
  • 打赏
  • 举报
回复
额 原串是 abc20150703551111.012txt 规律应该是 abcYYYYMMDD??1111.012txt 目的要通过传给程序一个正则表达式 把原串里面的??两位数字取出来
lniwn 2015-07-03
  • 打赏
  • 举报
回复
原始串是什么样?你要截取的子串“abc20150703551111.012txt”有何规律?例如:长度固定,格式固定。 把要求列清楚才能写对应的匹配表达式,至于你说的提取55,如果子串有规律,直接group就出来了。 所以前提是你要把原始串和提取串规律描述清楚。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧