|
比如:在字符串“0123456789asdfgh.dat”中找到“asd*.dat”
我是这样写的,但没得到我要的结果,输入asdfgh.dat可以匹配到,但输入asd*.dat就匹配不到。- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <regex.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/types.h>
- 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 match(const char *dir, const char *str)
- {
- int x;
- char *pattern;
- int z, cflags = 0;
- char ebuf[128], lbuf[256];
- regex_t reg;
- regmatch_t pm[10];
- const size_t nmatch = 10;
-
- pattern = (char *)malloc(sizeof(char));
-
- strcpy(pattern, str);
- z = regcomp(®, pattern, REG_EXTENDED);
-
- if (z != 0)
- {
- regerror(z, ®, ebuf, sizeof(ebuf));
- fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern);
- return -1;
- }
-
- z = regexec(®, dir, nmatch, pm, 0);
- if(z != 0)
- {
- regerror(z, ®, ebuf, sizeof(ebuf));
- fprintf(stderr, "%s: regcom('%s')\n", ebuf, dir);
- return -2;
- }
- for(x = 0; x < nmatch && pm[x].rm_so != -1; ++x)
- printf("result = %s\n", substr(dir, pm[x].rm_so, pm[x].rm_eo));
-
- regfree(®);
- return 0;
- }
- int main()
- {
- char *str = "lxy*.dat";
- char *data = "1234567890lxy2.dat";
- match(data, str);
- return 0;
- }
复制代码
想问问大家有什么好的方法? |
|