regex 简单使用

news/2024/7/7 20:09:03

目录

基本用法

匹配结果

迭代器

替换

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

迭代器

你还可以使用 std::sregex_iterator 迭代器来遍历匹配的所有结果:

std::regex pattern("\\b\\w+\\b"); // 匹配单词
std::string text = "This is a sample text.";
std::sregex_iterator iter(text.begin(), text.end(), pattern);
std::sregex_iterator end;

while (iter != end) {
    std::cout << "Match found: " << iter->str() << std::endl;
    ++iter;
}

替换

使用 std::regex_replace 进行字符串替换:

std::regex pattern("\\d+"); // 匹配一个或多个数字
std::string text = "The price is $42.";
std::string result = std::regex_replace(text, pattern, "[NUMBER]");
std::cout << "Result: " << result << std::endl;

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

#include <iostream>
#include <fstream>
#include <regex>
#include <string>

// Function to extract HTTPS links from a given text
std::vector<std::string> extractHttpsLinks(const std::string& text) {
    std::vector<std::string> links;
    // HTTPS link regex pattern
    std::regex linkRegex(R"(https:\/\/[^\s\[\]\(\)<>"',]+)");

    // Iterator for matching links
    std::sregex_iterator linkIterator(text.begin(), text.end(), linkRegex);
    std::sregex_iterator endIterator;

    // Extract matching links
    while (linkIterator != endIterator) {
        links.push_back(linkIterator->str());
        ++linkIterator;
    }

    return links;
}

// Function to read a file and extract HTTPS links
std::vector<std::string> extractHttpsLinksFromFile(const std::string& fileName) {
    std::ifstream file(fileName);
    std::string content;

    if (file.is_open()) {
        // Read the entire content of the file into a string
        content.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
        file.close();
    } else {
        std::cerr << "Unable to open file: " << fileName << std::endl;
    }

    // Extract HTTPS links from the file content
    return extractHttpsLinks(content);
}

int main(int argc, char* argv[]) {
    // Replace "your_cpp_file.cpp" with the path to your C++ file
    std::string cppFileName = argv[1];

    // Extract HTTPS links from the C++ file
    std::vector<std::string> httpsLinks = extractHttpsLinksFromFile(cppFileName);

    // Print the extracted HTTPS links
    std::cout << "HTTPS links in " << cppFileName << ":" << std::endl;
    for (const std::string& link : httpsLinks) {
        std::cout << link << std::endl;
    }

    return 0;
}

这是一个简单的正则表达式介绍和使用示例。实际使用中,正则表达式可以更复杂,支持字符集、分组、量词等高级特性,以满足不同的匹配需求。请注意,正则表达式中的某些字符可能需要进行转义,例如 \ 需要写作 \\


http://lihuaxi.xjx100.cn/news/1870785.html

相关文章

隧道施工废水工艺设备需要哪些

隧道施工废水工艺设备是保障隧道施工过程中废水处理的关键装备。它们能够有效处理施工废水中的悬浮物、悬浮油、重金属等污染物&#xff0c;确保废水排放符合相关环保标准。以下是隧道施工废水工艺设备常见的几种类型&#xff1a; 1. 隧道施工废水沉淀池&#xff1a;沉淀池是废…

Vue学习笔记-activated和deactivated生命周期

作用 路由组件所独有的2个生命周期 activated生命周期函数用于在路由组件每次由消失到出现时所调用的函数deactivated生命周期函数用于路由组件每次由出现到消失时&#xff08;无论是否缓存&#xff09;所调用的函数 案例 定义一个NewsVue组件&#xff0c;要求&#xff1a;…

基于人工智能技术《量化投资AI系统》的集群架构设计与实现

乔总&#xff1a; 前些日子你我的共同朋友潘总&#xff0c;推荐您来聊聊将ChatGPT应用于量化投资的合作。在与您及您的团队进行了超过2个多小时的沟通后&#xff0c;恕我直言&#xff0c;不客气地说&#xff0c;感觉您的团队对人工智能技术几乎是空白。为了让您的团队对人工智…

ARM预取侧信道(Prefetcher Side Channels)攻击与防御

目录 一、预取侧信道简介 1.1 背景:预取分类 二、Arm核会受到影响吗? 2.1 先进的预取器

MySQL三 | 多表查询

目录 多表查询 内连接 隐式内连接 显示内连接 外连接 左外连接 右外连接 自连接 子查询 多表查询 笛卡尔积:集合A和集合B的所有组合情况 A * B 在多表查询时应消除无效的笛卡尔积 内连接 查询的是两张表交集的地方 隐式内连接 SELECT 字段列表 FROM 表1&#xf…

80后土味英语引关注 专家称外语学习要尊重规律

近日“80后”胡振兴在非洲给员工开会&#xff0c;因其一口极具河南口音特色的“土味英语”引发关注。 据了解&#xff0c;胡振兴因外派工作中存在语言交流障碍于是开始零基础自学英语&#xff0c;在学习期间通过大量的听广播、看视频、请教身边的国际友人进行学习&#xff0c;…

Python创建交互式Web应用:Shiny库详解

更多资料获取 &#x1f4da; 个人网站&#xff1a;ipengtao.com Shiny是一个基于Python的交互式Web应用框架&#xff0c;专注于简化Web应用的开发流程。本文将深入探讨Shiny库的基本用法、高级功能以及实际应用案例&#xff0c;以帮助开发者充分发挥Shiny在Web应用开发中的优势…

【干货】顺序执行

方法1&#xff1a;脚本顺序执行 testFun001(){api.commonAjax666({}).then((res)>{if(res.code200){console.log("第一个执行&#xff01;")this.testFun002()}}) }, testFun002(){console.log("第二个执行&#xff01;") } 方法2&#xff1a;new Pro…