PCRE正则表达式库的编译和使用

本文介绍了如何下载、编译和安装PCRE库,并提供了使用PCRE库进行正则表达式匹配的实例,帮助开发者更好地理解和运用这个强大的库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值