http://www.psyon.org/projects/pcre-win32/index.php
PCRE库下载, 我在vs2008下 5.0静态库编译成功
#include <iostream> #include <string.h> #include "pcre.h" #pragma comment(lib,"pcre.lib") using namespace std; int fun_ismatch( char* src, char* pattern) { int ret; pcre *re; const char *error; int erroffset; int rc; pcre_extra *pcre_ex; do { if( (re = pcre_compile( pattern, 0, &error, &erroffset, NULL)) == NULL) { ret = -1; break; } pcre_ex = pcre_study(re, 0, &error); if( (rc = pcre_exec( re, pcre_ex, src, strlen(src), 0, 0, NULL, 0)) < 0) { ret = -1; break; } ret = rc; }while(0); free(re); return ret; } int main () { char *src="111 <title>Hello World</title> 222"; char *pattern="<title>(.*)</title>"; int ret; ret = fun_ismatch(src, pattern); if(ret != -1) cout<<"match"<<endl; else cout<<"not match"<<endl; return 0; }
#include <stdio.h> #include <string.h> #include <pcre.h> #define OVECCOUNT 30 /* should be a multiple of 3 */ #define EBUFLEN 128 #define BUFLEN 1024 int main() { pcre *re; const char *error; int erroffset; int ovector[OVECCOUNT]; int rc, i; char src [] = "111 <title>Hello World</title> 222"; char pattern [] = "<title>(.*)</title>"; 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); 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); } free(re); return 0; }