Poco正则表达式

本文通过C++代码示例介绍了如何使用Poco库中的正则表达式功能进行字符串匹配、替换及提取数字等操作。展示了extract、match与subst等方法的应用。

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

[hadoop@iZ25s7cmfyrZ C_script]$ cat poco_regularexpression.cpp 
#include <Poco/RegularExpression.h>
#include <iostream>

using Poco::RegularExpression;

int main(int argc, char** argv)
{
    RegularExpression p("[0-9]+");

    std::string numstr="12345ghk89760";
    std::string s;

    int n=p.extract(numstr,s);   //n=1, s=12345
    std::cout << n << std::endl;
    std::cout << s << std::endl;

    n=p.math(numstr); //  n=0;

    n=p.subst(numstr,"*");   //numstr="*ghk89760"

    RegularExpression::Match pos;
    n=p.match(numstr,0,pos);   // n=1;  pos.offset=0, pos.length = 5
    std::cout << n << std::endl;
    std::cout << pos.offset <<std::endl;
    std::cout << pos.length << std::endl;

    n=1;
    int of=0;
    while(n)    //0    12345 \n    5    89760   \n 13
    {
        n=p.match(numstr,of,pos);
        //p.subst(numstr,"*");
        p.extract(numstr,of,s);  
        std::cout << of << "\t" << s << std::endl;     
        of=pos.offset + pos.length;
    }

    n=1;
    while(n)            //"*ghk89760"   \n    "*ghk*"
    {
        n=p.subst(numstr,"*");
        std::cout << numstr << std::endl;
    }

}
 

转载于:https://my.oschina.net/lCQ3FC3/blog/807194

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值