Example of regular expression manipulation in C language

  • 2020-05-26 09:50:13
  • OfStack

This article illustrates regular expression operations in the C language. I will share it with you for your reference as follows:


#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc,char**argv)
{
 int status;
 int i;
 int cflags = REG_EXTENDED;
 regmatch_t pmatch[1];
 const size_t nmatch =1 ;
 regex_t reg;
 const char * pattern="^[A-Z]{2}\\w+@\\w{6}_\\w+.\\w+$";
 //const char * pattern="^[A-Z]{2}\\w+$";
 //const char * pattern="^\\w$";
 regcomp(®,pattern,cflags);
 status=regexec(®,argv[1],nmatch,pmatch,0);
 printf("%s",argv[1]);
 if(status == REG_NOMATCH)
 printf("no Match\n");
 else if(status ==0)
 {
 printf("match\n");
 }
}

Matches strings like admin@tools_ofstack.com

PS: here are two more handy regular expression tools for you to use:

JavaScript regular expression online testing tool:
http://tools.ofstack.com/regex/javascript

Online regular expression generation tool:
http://tools.ofstack.com/regex/create_reg

I hope this article has been helpful to you in programming C.


Related articles: