Skip to content

Instantly share code, notes, and snippets.

@clarkzjw
Created November 1, 2014 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkzjw/5244046565050a01624e to your computer and use it in GitHub Desktop.
Save clarkzjw/5244046565050a01624e to your computer and use it in GitHub Desktop.
regex in C/C++
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc,char** argv)
{
int status,i;
int cflags=REG_EXTENDED;
regmatch_t pmatch[1];
const size_t nmatch=1;
regex_t reg;
const char *pattern ="^\\w(\\.?\\w)+@\\w+(\\.\\w+)*\\.[A-Za-z]+$";
char buf[256];
regcomp(&reg, pattern,cflags);
while(scanf("%s", buf)!=EOF)
{
if((status=regexec(&reg,buf,nmatch,pmatch,0))==0)
{
printf("YES\n");
regfree(&reg);
}
else
{
printf("NO\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment