linux C regex正则表达式函数库
{
const char *regex = "href=\"[^ >]*\"";
regex_t preg;
const size_t nmatch = 10;
regmatch_t pm[nmatch];
if (regcomp(&preg, regex, 0) != 0) { /*编译正则表达式失败 */
perror("regcomp");
exit(1);
}
int z, i;
z = regexec(&preg, buf, nmatch, pm, 0);
if (z == REG_NOMATCH)/*无匹配项 */
{
return 0;
}
else/*有匹配的超链接 */
{
for (i = 0; i < nmatch && pm[i].rm_so != -1; ++i)/*把超链接都提取出*/
{
/*对匹配链接的操作*/
}
}
regfree(&preg);/*释放正则表达式*/
}
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
#include <string.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 main(int argc, char** argv)
{
char * pattern;
int x, z, lno = 0, cflags = 0;
char ebuf[128], lbuf[256];
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
pattern = argv[1];
z = regcomp(®, pattern, cflags);
if (z != 0)
{
regerror(z, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: pattern '%s' \n", ebuf, pattern);
return 1;
}
while(fgets(lbuf, sizeof(lbuf), stdin))
{
++lno;
if ((z = strlen(lbuf)) > 0 && lbuf[z-1] == '\n')
lbuf[z - 1] = 0;
z = regexec(®, lbuf, nmatch, pm, 0);
if (z == REG_NOMATCH) continue;
else if (z != 0)
{
regerror(z, ®, ebuf, sizeof(ebuf));
fprintf(stderr, "%s: regcom('%s')\n", ebuf, lbuf);
return 2;
}
for (x = 0; x < nmatch && pm[x].rm_so != -1; ++ x)
{
if (!x) printf("d: %s\n", lno, lbuf);
printf(" $%d='%s'\n", x, substr(lbuf, pm[x].rm_so, pm[x].rm_eo));
}
}
regfree(®);
return 0;
}
编译执行
bitwangbin@mac:~/code/c/regex > gcc regexp.c -o regexp
bitwangbin@mac:~/code/c/regex > ./regexp 'regex[a-z]*' < regexp.c
0003: #include <regex.h>;
$0='regex'
0020: regex_t reg;
$0='regex'
0037: z = regexec(®, lbuf, nmatch, pm, 0);
$0='regexec'