LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 860|回复: 13

字符串匹配的问题

[复制链接]
发表于 2007-3-27 17:09:38 | 显示全部楼层 |阅读模式
比如:在字符串“0123456789asdfgh.dat”中找到“asd*.dat”
我是这样写的,但没得到我要的结果,输入asdfgh.dat可以匹配到,但输入asd*.dat就匹配不到。
  1. #include <stdio.h>
  2. #include <stdlib.h>   
  3. #include <sys/types.h>   
  4. #include <regex.h>   
  5. #include <string.h>
  6. #include <dirent.h>
  7. #include <sys/types.h>
  8. static char* substr(const char *str, unsigned start, unsigned end)   
  9. {
  10.         unsigned n = end - start;   
  11.         static char stbuf[256];   
  12.         strncpy(stbuf, str + start, n);   
  13.         stbuf[n] = 0;   
  14.         return stbuf;   
  15. }   
  16. int match(const char *dir, const char *str)
  17. {
  18.         int x;
  19.         char *pattern;
  20.         int z, cflags = 0;
  21.         char ebuf[128], lbuf[256];  
  22.         regex_t reg;   
  23.         regmatch_t pm[10];   
  24.         const size_t nmatch = 10;       
  25.        
  26.         pattern = (char *)malloc(sizeof(char));
  27.        
  28.         strcpy(pattern, str);
  29.         z = regcomp(&reg, pattern, REG_EXTENDED);
  30.        
  31.         if (z != 0)
  32.         {
  33.                 regerror(z, &reg, ebuf, sizeof(ebuf));
  34.                 fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern);
  35.                 return -1;
  36.         }
  37.        
  38.         z = regexec(&reg, dir, nmatch, pm, 0);
  39.         if(z != 0)   
  40.         {   
  41.                 regerror(z, &reg, ebuf, sizeof(ebuf));   
  42.                 fprintf(stderr, "%s: regcom('%s')\n", ebuf, dir);   
  43.                 return -2;   
  44.         }   
  45.         for(x = 0; x < nmatch && pm[x].rm_so != -1; ++x)         
  46.                 printf("result = %s\n", substr(dir, pm[x].rm_so, pm[x].rm_eo));   
  47.                
  48.         regfree(&reg);
  49.         return 0;
  50. }
  51. int main()
  52. {
  53.         char *str = "lxy*.dat";
  54.         char *data = "1234567890lxy2.dat";
  55.         match(data, str);
  56.         return 0;
  57. }
复制代码


想问问大家有什么好的方法?
发表于 2007-3-27 17:46:59 | 显示全部楼层
请用[HTML][CODE][/CODE][/HTML]将代码括起来再发一次吧!
看起来会顺眼一点,谢谢!
回复 支持 反对

使用道具 举报

发表于 2007-3-28 00:39:01 | 显示全部楼层
pattern = (char *)malloc(sizeof(char));
       
        strcpy(pattern, str);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-28 08:10:57 | 显示全部楼层
什么意思啊?
回复 支持 反对

使用道具 举报

发表于 2007-3-28 09:11:37 | 显示全部楼层
为什么是sizeof(char)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-28 10:29:02 | 显示全部楼层
我觉得这个没太大关系,只是给pattern分配空间。
回复 支持 反对

使用道具 举报

发表于 2007-3-28 10:31:08 | 显示全部楼层
分配了多少空间??
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-28 11:10:21 | 显示全部楼层
就是sizeof(char)这么大啊,这个有问题吗?
回复 支持 反对

使用道具 举报

发表于 2007-3-28 11:22:23 | 显示全部楼层
Post by lixiaoya
就是sizeof(char)这么大啊,这个有问题吗?

分配多大取决于需要多大. 楼主请思考:
* sizeof(char) 究竟是多大;
* 在楼主的代码段中, 究竟需要多大的空间才保证不溢出
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-3-28 11:43:32 | 显示全部楼层
哦,谢谢你们的建议,我再考虑考虑,先说说有什么好的办法匹配lxy*.dat这种形式的字符串吧。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表