C++标准库中提供的用于处理正则表达式的类std::regex

std 是 C++ 标准库的命名空间,包含了大量标准的 C++ 类、函数和对象。这些类和函数提供了广泛的功能,包括输入输出、容器、算法、字符串处理等。

通常,为了使用标准库中的对象和函数,需在代码中包含相应的头文件,比如 #include <iostream>。然后你就可以通过 std:: 前缀来使用其中的功能,比如 std::coutstd::cinstd::endl 等。

这种用法有助于防止命名冲突因为 C++ 中可能会有多个库提供相同的名称。使用命名空间可以明确指定要使用的是标准库中的功能,而不是其他地方定义的同名功能。

C++ 标准库中常见的类、函数等:

1. 类:
   - `std::string`: 字符串处理类。
   - `std::vector`: 动态数组容器类。
   - `std::map`、`std::unordered_map`: 键值对映射容器类。
   - `std::fstream`: 文件输入输出类。
   - `std::deque`: 双端队列容器类。
   - `std::set`、`std::unordered_set`: 集合容器类。
   - `std::stack`、`std::queue`: 栈和队列容器适配器类。
   - `std::stringstream`: 字符串流类。

2. 函数:
   - `std::cout`、`std::cerr`: 控制台输出函数。
   - `std::cin`: 控制台输入函数。
   - `std::sort`: 容器排序函数。
   - `std::find`: 容器查找函数。
   - `std::max`、`std::min`: 返回两个值中较大或较小的值。
   - `std::accumulate`: 容器元素累加函数。
   - `std::copy`: 复制范围内元素到另一个范围函数。
   - `std::transform`: 容器元素转换函数。
   - `std::regex_search`: 正则表达式搜索函数。
   - `std::regex_match`: 正则表达式匹配函数。
   - `std::regex_replace`: 正则表达式替换函数。

3. 对象:
   - `std::endl`: 换行并刷新输出流对象。
   - `std::numeric_limits`: 数值类型极限值信息对象。
   - `std::allocator`: 动态内存分配器对象。
   - `std::cin.eof()`: 输入流对象函数,检查是否达到文件结束。
   - `std::nothrow`: 内存分配失败时返回空指针而不抛出异常的对象。
   - `std::random_device`: 真随机数生成对象。
   - `std::locale`: 控制 C++ 标准库本地化行为的对象。

这些类、函数和对象提供了丰富的功能,覆盖了输入输出、容器、算法、字符串处理、正则表达式等多个方面,为 C++ 程序员提供了强大的工具,可用于各种类型的应用开发。

------------

`std::regex` 是 C++ 标准库中提供的用于处理正则表达式的类。正则表达式是一种强大的模式匹配工具,它可以用于在字符串中进行复杂的搜索、替换等操作。`std::regex` 类提供了一种方式来创建、编译和使用正则表达式。

下面是 `std::regex` 类的一些重要成员函数和用法:

1. 构造函数:
   - `explicit regex(const char* fmt, flag_type flags = std::regex_constants::ECMAScript)`
   - `explicit regex(const std::string& fmt, flag_type flags = std::regex_constants::ECMAScript)`
   
   这些构造函数用于创建 `std::regex` 对象,接受一个正则表达式字符串作为参数,并可选择地指定匹配标志。

2. 成员函数 `match()` 和 `search()`:
   - `bool match(const std::string& s, std::regex_constants::match_flag_type flags = std::regex_constants::match_default) const`
   - `bool search(const std::string& s, std::regex_constants::match_flag_type flags = std::regex_constants::match_default) const`
   
   这两个成员函数分别用于在字符串中进行完全匹配(`match()`)和部分匹配(`search()`)。它们接受一个待匹配的字符串作为参数,并可选择地指定匹配标志。

3. 替换函数 `std::regex_replace()`:
   - `std::string regex_replace(InputIt first, InputIt last, const std::regex& re, const std::string& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default)`

   这个函数用于在范围 `[first, last)` 中搜索并替换满足正则表达式 `re` 的部分。替换的方式由参数 `fmt` 指定。

4. 正则表达式的语法:
   
   `std::regex` 支持多种正则表达式的语法,包括 ECMAScript、basic、extended 等等。你可以通过设置不同的标志来指定使用的语法。常见的标志包括:
   - `std::regex_constants::ECMAScript`:使用 ECMAScript 语法。
   - `std::regex_constants::basic`:使用基本正则表达式语法。
   - `std::regex_constants::extended`:使用扩展正则表达式语法。

这些只是 `std::regex` 类的一些常用成员函数和用法。借助这些函数,可方便地在字符串中进行正则表达式的搜索、替换等操作,实现了复杂文本处理的功能。

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

int main() {
    // 原始字符串
    std::string text = "Hello, world!";

    // 定义正则表达式模式
    std::regex pattern("world");

    // 在文本中搜索模式
    if (std::regex_search(text, pattern)) {
        std::cout << "在文本中找到了匹配的模式!" << std::endl;
    }
    else {
        std::cout << "未找到匹配的模式!" << std::endl;
    }

    return 0;
}

/*std::regex:表示一个正则表达式对象。
std::smatch:保存匹配结果的容器,可以通过 std::regex_match 或 std::regex_search 函数填充。
std::regex_match:用于检查整个字符串是否与正则表达式匹配。
std::regex_search:在输入字符串中搜索与正则表达式匹配的内容。
std::regex_replace:用于在字符串中执行正则表达式替换操作。
std::regex_iterator:用于迭代一个字符串中所有与正则表达式匹配的子串。
std::regex_token_iterator:用于迭代一个字符串中与正则表达式匹配的子串及其非匹配部分。*/ 

#include <iostream>
#include <regex>

int main() {
    std::string text = "Hello, my email is example@email.com and my phone number is 123-456-7890.";
    std::regex emailRegex("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
    std::regex phoneRegex("\\d{3}-\\d{3}-\\d{4}");

    // Match email
    std::smatch emailMatch;
    if (std::regex_search(text, emailMatch, emailRegex)) {
        std::cout << "Email found: " << emailMatch.str() << std::endl;
    }

    // Match phone number
    std::smatch phoneMatch;
    if (std::regex_search(text, phoneMatch, phoneRegex)) {
        std::cout << "Phone number found: " << phoneMatch.str() << std::endl;
    }

    // Replace phone number
    std::string newText = std::regex_replace(text, phoneRegex, "XXX-XXX-XXXX");
    std::cout << "Text with phone number replaced: " << newText << std::endl;

    return 0;
}

/*std::regex:表示一个正则表达式对象。
std::smatch:保存匹配结果的容器,可以通过 std::regex_match 或 std::regex_search 函数填充。
std::regex_match:用于检查整个字符串是否与正则表达式匹配。
std::regex_search:在输入字符串中搜索与正则表达式匹配的内容。
std::regex_replace:用于在字符串中执行正则表达式替换操作。
std::regex_iterator:用于迭代一个字符串中所有与正则表达式匹配的子串。
std::regex_token_iterator:用于迭代一个字符串中与正则表达式匹配的子串及其非匹配部分。*/

相关推荐

  1. Qt表达用法

    2024-03-27 10:22:10       44 阅读
  2. 表达规则

    2024-03-27 10:22:10       62 阅读
  3. 02_表达应用

    2024-03-27 10:22:10       49 阅读
  4. 表达妙用】

    2024-03-27 10:22:10       57 阅读
  5. 表达应用

    2024-03-27 10:22:10       40 阅读
  6. Harmony 表达写法

    2024-03-27 10:22:10       38 阅读
  7. 表达常见语法

    2024-03-27 10:22:10       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 10:22:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 10:22:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 10:22:10       82 阅读
  4. Python语言-面向对象

    2024-03-27 10:22:10       91 阅读

热门阅读

  1. 正则表达式:深入理解与应用

    2024-03-27 10:22:10       39 阅读
  2. CentOS 7 安装 Git

    2024-03-27 10:22:10       42 阅读
  3. 11. Linux中进程控制细节

    2024-03-27 10:22:10       38 阅读
  4. 【算法】计数排序

    2024-03-27 10:22:10       38 阅读
  5. 算法打卡day18

    2024-03-27 10:22:10       43 阅读
  6. 握手和挥手

    2024-03-27 10:22:10       39 阅读
  7. npm常用命令详解

    2024-03-27 10:22:10       39 阅读
  8. Excel 导入、导出的封装

    2024-03-27 10:22:10       37 阅读
  9. 【go-工具】pprof

    2024-03-27 10:22:10       34 阅读
  10. 如何获取iOS手机上的APP崩溃日志?

    2024-03-27 10:22:10       34 阅读