regex 简单使用

本文介绍了C++11引入的正则表达式库regex,它可在字符串中进行模式匹配。文中阐述了regex的基本用法,如构造对象、匹配操作,还提及匹配结果、迭代器、替换等内容,最后给出提取文件中https链接的示例。

目录

基本用法

匹配结果

迭代器

替换

regex小试牛刀 ,提取文件中所有https链接

std::regex 是 C++11 引入的正则表达式库,它允许在字符串中进行模式匹配。正则表达式是一种强大的字符串匹配工具,允许你使用模式来描述你感兴趣的字符串集。

以下是一些常见的 std::regex 的用法,以及如何在 C++ 中使用它们:

基本用法

  1. 构造正则表达式对象:

    std::regex pattern("hello");
  2. 使用正则表达式进行匹配:

    std::string text = "hello world";
    if (std::regex_search(text, pattern)) {
        std::cout << "Match found!" << std::endl;
    } else {
        std::cout << "No match found." << std::endl;
    }

匹配结果

std::smatch 类型用于保存匹配结果,你可以使用它来获取匹配的子串。

std::regex pattern("\\d+"); // 匹配一个或多个数字
std::string text = "The price is $42.";
std::smatch match;

if (std::regex_search(text, match, pattern)) {
    std::cout << "Match found: " << match.str() << std::endl;
} else {
    std::cout << "No match found." << std::
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

telllong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值