免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2222 | 回复: 12
打印 上一主题 下一主题

关于regexp? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-09-13 14:38 |只看该作者 |倒序浏览

  1. #include <stdio.h>;
  2. #include <stdlib.h>;
  3. #include <regexp.h>;

  4. int main()
  5. {
  6.    int i;
  7.    regexp *re = regcomp("ab{1,3}c");
  8.    i = regexec(re,"abc");
  9.    if(i==1)
  10.       puts("ok");
  11.    else
  12.       puts("not ok");
  13. }
复制代码


环境:FreeBSD4.10
为什么不是输出“ok”?

论坛徽章:
0
2 [报告]
发表于 2004-09-13 15:00 |只看该作者

关于regexp?

你用的是否标准 regex ?
man regexec 看看。
[code]nRet = regexec ( &Var, szLine, nMatch, regMatch, 0 );
   if ( nRet == REG_NOMATCH)                       ///< 不匹配
   {
      regfree(&Var);
      return false;
   }
   else if ( nRet != 0 )                           ///< 执行匹配出错
   {
      regerror ( nRet, &Var, szErr, sizeof(szErr) );
      TraceLog ( stdout, __LINE__, "", __FILE__, "Line %s, regexec error %s\n", szLine, szErr );
      regfree(&Var);
      return false;
   }
   
   if ( szValue != NULL )                          ///< 获取子字符串
   {
      int nLen = regMatch[0].rm_eo - regMatch[0].rm_so;
      strncpy ( szValue, szLine + regMatch[0].rm_so, nLen );
      szValue[nLen] = '\0';
   }
   
   regfree(&Var);                               /// 释放正则表达式
   return true;[/code]

论坛徽章:
0
3 [报告]
发表于 2004-09-13 15:17 |只看该作者

关于regexp?

老兄,我用的FreeBSD4.10,

REGEXP(3)              FreeBSD Library Functions Manual              REGEXP(3)

NAME
     regcomp, regexec, regsub, regerror -- regular expression handlers

LIBRARY
     Compatibility Library (libcompat, -lcompat)

SYNOPSIS
     #include <regexp.h>;

     regexp *
     regcomp(const char *exp);

     int
     regexec(const regexp *prog, const char *string);

     void
     regsub(const regexp *prog, const char *source, char *dest);

DESCRIPTION
     This interface is made obsolete by regex(3).

论坛徽章:
0
4 [报告]
发表于 2004-09-13 15:20 |只看该作者

关于regexp?

看看regerror 吧。

论坛徽章:
0
5 [报告]
发表于 2004-09-13 15:27 |只看该作者

关于regexp?

see manpage of regexec:

regexec returns zero for a successful match or REG_NOMATCH for failure

能打出OK吗


原帖由 "thoujean" 发表:

环境:FreeBSD4.10
为什么不是输出“ok”?

论坛徽章:
0
6 [报告]
发表于 2004-09-13 15:31 |只看该作者

关于regexp?

楼上正解。

论坛徽章:
0
7 [报告]
发表于 2004-09-13 15:51 |只看该作者

关于regexp?


  1. include <stdio.h>;
  2. #include <stdlib.h>;
  3. #include <regexp.h>;

  4. int main()
  5. {
  6.         int i;
  7.         regexp *re = regcomp("abc");
  8.         i = regexec(re,"abc");
  9.         if(i==1)
  10.                 puts("ok");
  11.         else
  12.                 puts("not ok");
  13. }
复制代码


cc re.c -lcompat
a.out
ok

相信了吗?

论坛徽章:
0
8 [报告]
发表于 2004-09-13 15:54 |只看该作者

关于regexp?

REGEXP(3)              FreeBSD Library Functions Manual              REGEXP(3)



The regexec() function matches a NUL-terminated string against the com-
     piled regular expression in prog.  It returns 1 for success and 0 for
     failure, and adjusts the contents of prog's startp and endp (see below)
     accordingly.

论坛徽章:
0
9 [报告]
发表于 2004-09-13 15:57 |只看该作者

关于regexp?

相信啥?

regexec 返回的 1 是 REG_NOMATCH, 所以你打出OK。

论坛徽章:
0
10 [报告]
发表于 2004-09-13 16:01 |只看该作者

关于regexp?

那FREEBSD上的,你根据函数的说明就对了。我们看的不是FREEBSD.
所以你是对的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP