C语言正则表达式的使用(二)

2014-04-06 17:41:53 · 作者: · 浏览: 327

 

  以下是例子:(环境 centos)

  /*  write by gx

  *  time:2014/3/21

  *  for: test regex

  *  */

  #include <REGEX.H>

  #include <SYS types.h>

  #include <STDIO.H>

  int main(int argc,char** argv)

  {

  int status;

  int i;

  int cflags = REG_EXTENDED;

  regmatch_t pmatch ;

  const size_t nmatch = 1;

  regex_t reg;

  const char* pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*$";

  char* buf = "helloworld12345@qq.com";

  regcomp( , pattern, cflags );

  status = regexec( , buf, nmatch, pmatch, 0 );

  if( REG_NOMATCH == status )

  {

  printf("-----no nomatch--------");

  return 0;

  }

  if( status == 0 )

  {

  printf("match:\n");

  for( i=pmatch[0].rm_so; i<PMATCH[0].RM_EO; pre }< 0; return regfree(?); } printf(?\n ); putchar(buf[i]); { ) i++><BR>

  <P><BR>

  </P>