Skip to content

Instantly share code, notes, and snippets.

@yukioc
Created February 12, 2012 02:25
Show Gist options
  • Save yukioc/1805850 to your computer and use it in GitHub Desktop.
Save yukioc/1805850 to your computer and use it in GitHub Desktop.
posix regex example code
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
int main(int argc,char*argv[]){
char b[1024],i=-1,n=0;
regex_t re;
regmatch_t m[9];
if(argc==1)return 1;
regcomp(&re,argv[1],REG_EXTENDED|REG_NEWLINE);
if(argc>2) n=atoi(argv[2]);
while(fgets(b,1024,stdin)!='\0'){
if(regexec(&re,b,n,m,0)!=REG_NOMATCH){
for(i=0;i<n;i++){
if(i>0)putchar(',');
while(m[i].rm_so!=m[i].rm_eo)putchar(b[m[i].rm_so++]);
}
putchar('\n');
}
}
regfree(&re);
return (i<0)?1:0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment