1、PCRE库下载:
http://www.pcre.org/
2、编译和安装
http://blog.youkuaiyun.com/jollyjumper/article/details/5700024
http://www.cppblog.com/woaidongmao/archive/2009/09/07/95493.html
3、PCRE的使用实例
#include <..\pcre\include\pcre.h>
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring sSrcReal = L"ahsf(agsh艾丝凡);asghjgah(ahsjhj);(&*(^&&*AS%Q(asg1asg); ajshfghasfg";
std::string sSrcRealUtf_8 = CW2A(sSrcReal.c_str(), CP_UTF8);
// 使用PCRE提取用户ID
std::string sRulesUtf_8 = CW2A(L"(?<=\\()[\\w\u4E00-\u9FFF]+(?=\\);)", CP_UTF8);
int nOptions = PCRE_EXTENDED | PCRE_UTF8;
const char *pError;
int nErroffset;
// 构造pcre结构
pcre *pRe = pcre_compile(
sRulesUtf_8.c_str(),
nOptions,
&pError, /* for error message */
&nErroffset, /* for error offset */
NULL); /* use default character tables */
std::vector<std::wstring> sPcreOut;
int nPos = 0;
int nRet = 1;
while(pRe && nRet > 0)
{
int ovector[30]; // 必须为3的整数倍
memset(&ovector, -1, sizeof(ovector));
// 执行正则表达式匹配
nRet = pcre_exec(
pRe, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
sSrcRealUtf_8.c_str(),//subject, /* the subject string */
(int)sSrcRealUtf_8.size(), /* the length of the subject */
nPos, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
sizeof(ovector) / sizeof(ovector[0])); /* number of elements in the output vector */
// ovector[0]存储匹配的首字符位置,ovector[1]存储匹配字符串后的第一个不匹配的字符位置
if(nRet > 0)
{
nPos = ovector[1];
std::string sSub = sSrcRealUtf_8.substr(ovector[0], nPos - ovector[0]);
std::wstring sTemp = CA2W(sSub.c_str(), CP_UTF8);
sPcreOut.push_back(sTemp);
}
}
return 0;
}