使用pcre库正则表达式处理非ASCII码时出现乱码的问题
使用pcre库正则表达式处理非ASCII码时出现乱码的问题
pcre下载地址 http://www.psyon.org/projects/pcre-win32/index.php
我使用的是最后一个
代码如下
// pcre79.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "pcre.h"#include <stdio.h> #include <string.h> #define OVECCOUNT 30 /* should be a multiple of 3 */int main(int argc, char* argv[]){ pcre *re; const char *error; int erroffset; int ovector[OVECCOUNT]; int rc,i; char src[]="123:156"; //进行匹配的字符 char pattern[]="[^123(:|:|( )*)?].*"; //正则表达式 printf("string:%s\n",src); printf("pattern:%s\n",pattern); re=pcre_compile(pattern,0,&error,&erroffset,NULL); if(re==NULL){ printf("pcre compilation failed at offset %d:%s\n",erroffset,error); return 1; } rc=pcre_exec(re,NULL,src,strlen(src),0,0,ovector,OVECCOUNT); if(rc<0){ if(rc==PCRE_ERROR_NOMATCH){ printf("sorry ,no match ...\n"); } else{ printf("matching error %d\n",rc); pcre_free(re); return 1; } } printf("\nok ,has matched ...\n\n"); for(i=0;i<rc;i++){ char *substring_start=src+ovector[2*i]; int substring_length=ovector[2*i+1]-ovector[2*i]; printf("$%2d :%.*s\n",i,substring_length,substring_start); } pcre_free(re); return 0; }char src[]="123:156"; //进行匹配的字符 char pattern[]="[^123(:|( )*)?].*"; //正则表达式
char src[]="123:的56"; //进行匹配的字符 char pattern[]="[^123(:|:|( )*)?].*"; //正则表达式