简介

  • C++ 标准库

C++ 标准库

<regex> 是 C++11 标准库中提供的头文件,用于处理正则表达式。它提供了一系列类和函数,使得在 C++ 程序中可以进行正则表达式的匹配、搜索和替换等操作。

主要类和函数:

  1. std::regex:表示一个正则表达式对象。

  2. std::regex_match():用于检查整个字符串是否与正则表达式匹配。

  3. std::regex_search():在给定字符串中搜索正则表达式的匹配项。

  4. std::regex_replace():使用正则表达式进行替换操作。

  5. std::smatchstd::cmatch:表示正则表达式匹配结果的容器,分别用于 std::stringconst char* 类型的字符串。

  6. std::regex_iterator:用于在字符串中迭代查找匹配的正则表达式。

示例用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("fox");

    // 检查整个字符串是否与正则表达式匹配
    if (std::regex_search(text, pattern)) {
        std::cout << "Match found!" << std::endl;
    } else {
        std::cout << "Match not found!" << std::endl;
    }

    // 使用正则表达式进行替换操作
    std::string replacedText = std::regex_replace(text, pattern, "cat");
    std::cout << "Replaced text: " << replacedText << std::endl;

    return 0;
}

在这个示例中,<regex> 头文件中的函数和类被用于查找匹配的字符串并进行替换。std::regex_search() 用于检查是否存在匹配项,而 std::regex_replace() 则用于替换匹配的部分。

C++ 标准库 详解

C++ 标准库中的 <regex> 头文件提供了对正则表达式的支持,允许进行字符串的匹配、搜索和替换等操作。在 C++11 标准中引入了正则表达式库,它提供了一系列类和函数用于处理正则表达式。

主要类和函数:

  1. std::regex:表示一个正则表达式对象。可以创建一个 std::regex 对象,用于表示要匹配的正则表达式模式。

    1
    
     std::regex pattern("your_pattern_here");
    
  2. std::regex_match():用于检查整个字符串是否与正则表达式模式匹配。

    1
    2
    
     std::string text = "your_text_here";
     bool isMatch = std::regex_match(text, pattern);
    
  3. std::regex_search():在给定的字符串中搜索正则表达式模式的匹配项。

    1
    2
    
     std::string text = "your_text_here";
     bool isMatch = std::regex_search(text, pattern);
    
  4. std::regex_replace():使用正则表达式模式进行替换操作。

    1
    2
    
     std::string text = "your_text_here";
     std::string replacedText = std::regex_replace(text, pattern, "replacement_text");
    
  5. std::smatchstd::cmatch:表示正则表达式匹配结果的容器,分别用于 std::stringconst char* 类型的字符串。

    1
    2
    3
    
     std::string text = "your_text_here";
     std::smatch match;
     std::regex_match(text, match, pattern);
    
  6. std::regex_iterator:用于在字符串中迭代查找正则表达式模式的匹配项。

    1
    2
    3
    4
    5
    6
    
     std::string text = "your_text_here";
     std::regex_iterator<std::string::iterator> it(text.begin(), text.end(), pattern);
     std::regex_iterator<std::string::iterator> end;
     for (; it != end; ++it) {
         std::cout << "Match found: " << it->str() << std::endl;
     }
    

这些函数和类提供了对正则表达式的基本支持,可以在 C++ 程序中执行各种与正则表达式相关的操作,包括匹配、搜索和替换等。

C++ 标准库 常用的类和函数

C++ 标准库 <regex> 中包含了一些常用的类和函数,用于处理正则表达式。以下是其中一些主要的类和函数:

主要类:

  1. std::regex:表示一个正则表达式对象。

  2. std::smatchstd::cmatch:表示正则表达式匹配结果的容器,分别用于 std::stringconst char* 类型的字符串。

  3. std::regex_iterator:用于在字符串中迭代查找正则表达式的匹配项。

主要函数:

  1. std::regex_match():用于检查整个字符串是否与正则表达式匹配。

  2. std::regex_search():在给定字符串中搜索正则表达式的匹配项。

  3. std::regex_replace():使用正则表达式进行替换操作。

  4. std::regex_constants:提供了一些常量,用于设置正则表达式的标志和选项。

这些类和函数允许在 C++ 中对字符串执行正则表达式的匹配、搜索、替换等操作,为处理文本提供了强大的工具。

std::regex

std::regex 是 C++ 标准库 <regex> 中的类,用于表示一个正则表达式对象。它允许在 C++ 程序中创建和使用正则表达式模式,并用于字符串的匹配、搜索和替换等操作。

主要功能:

  1. 构造和使用正则表达式对象std::regex 可以使用正则表达式字符串构造对象,表示要匹配的模式。

    1
    
     std::regex pattern("your_pattern_here");
    
  2. 匹配检查std::regex_match() 函数用于检查整个字符串是否与正则表达式模式匹配。

    1
    2
    
     std::string text = "your_text_here";
     bool isMatch = std::regex_match(text, pattern);
    
  3. 搜索匹配项std::regex_search() 函数在给定的字符串中搜索正则表达式模式的匹配项。

    1
    2
    
     std::string text = "your_text_here";
     bool isMatch = std::regex_search(text, pattern);
    
  4. 替换操作std::regex_replace() 函数使用正则表达式模式进行替换操作。

    1
    2
    
     std::string text = "your_text_here";
     std::string replacedText = std::regex_replace(text, pattern, "replacement_text");
    

std::regex 类提供了对正则表达式模式的支持,并允许在 C++ 中对字符串进行基于模式的匹配、搜索和替换等操作。

std::smatch

std::smatch 是 C++ 标准库 <regex> 中的类,用于存储正则表达式的匹配结果。它是一个模板类,用于存储 std::string 类型的匹配结果。

主要特点和用法:

  1. 存储匹配结果std::smatch 类型的对象可以存储正则表达式匹配到的结果,以便进一步处理。

  2. 作为 std::regex_match() 和 std::regex_search() 的参数:通常作为 std::regex_match()std::regex_search() 函数的额外参数,用于存储匹配的结果。

  3. 访问匹配子字符串:通过索引或迭代器等方式可以访问匹配到的子字符串。

示例用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("\\b\\w{4}\\b"); // 匹配长度为4的单词

    std::smatch match;

    if (std::regex_search(text, match, pattern)) {
        std::cout << "Match found!" << std::endl;

        // 遍历匹配到的结果
        for (auto it = match.begin(); it != match.end(); ++it) {
            std::cout << "Matched substring: " << *it << std::endl;
        }
    } else {
        std::cout << "No match found!" << std::endl;
    }

    return 0;
}

在这个示例中,std::smatch 类型的对象 match 用于存储正则表达式模式匹配到的结果。通过 std::regex_search() 函数将匹配结果存储在 match 对象中,并可以通过迭代器或索引访问匹配到的子字符串。

std::cmatch

std::cmatch 是 C++ 标准库 <regex> 中的类,用于存储正则表达式的匹配结果。不同于 std::smatchstd::cmatch 专门用于存储匹配 C 风格字符串(const char* 类型)的结果。

主要特点和用法:

  1. 存储匹配结果std::cmatch 类型的对象可以存储正则表达式匹配到的 C 风格字符串(const char* 类型)的结果。

  2. 作为 std::regex_match() 和 std::regex_search() 的参数:通常作为 std::regex_match()std::regex_search() 函数的额外参数,用于存储匹配的结果。

  3. 访问匹配子字符串:通过索引或迭代器等方式可以访问匹配到的子字符串。

示例用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <regex>

int main() {
    const char* text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("\\b\\w{4}\\b"); // 匹配长度为4的单词

    std::cmatch match;

    if (std::regex_search(text, match, pattern)) {
        std::cout << "Match found!" << std::endl;

        // 遍历匹配到的结果
        for (auto it = match.begin(); it != match.end(); ++it) {
            std::cout << "Matched substring: " << *it << std::endl;
        }
    } else {
        std::cout << "No match found!" << std::endl;
    }

    return 0;
}

在这个示例中,std::cmatch 类型的对象 match 用于存储正则表达式模式匹配到的 C 风格字符串的结果。通过 std::regex_search() 函数将匹配结果存储在 match 对象中,并可以通过迭代器或索引访问匹配到的子字符串。

std::regex_iterator

std::regex_iterator 是 C++ 标准库 <regex> 中的类,用于在字符串中迭代查找正则表达式的匹配项。

主要特点和用法:

  1. 在字符串中迭代查找匹配项std::regex_iterator 可以在给定的字符串中按顺序迭代查找正则表达式的所有匹配项。

  2. 作为 std::sregex_iterator 和 std::cregex_iterator 的实例std::regex_iterator 实际上是 std::sregex_iterator(用于 std::string)和 std::cregex_iterator(用于 const char*)的实例。

  3. 迭代器遍历匹配结果:通过迭代器可以逐个访问匹配的子字符串。

示例用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("\\b\\w{4}\\b"); // 匹配长度为4的单词

    std::sregex_iterator it(text.begin(), text.end(), pattern); // 用于 std::string 的迭代器
    std::sregex_iterator end; // 结束迭代器

    for (; it != end; ++it) {
        std::cout << "Matched substring: " << it->str() << std::endl;
    }

    return 0;
}

在这个示例中,std::sregex_iterator 被用来在给定的 std::string 中迭代查找正则表达式模式的匹配项。std::sregex_iterator 构造函数的参数是要进行匹配的字符串的迭代器范围和正则表达式模式,然后通过迭代器逐个访问匹配的子字符串。

std::regex_match()

std::regex_match() 是 C++ 标准库 <regex> 中的函数,用于检查整个字符串是否与给定的正则表达式模式匹配。

用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("quick");

    bool isMatch = std::regex_match(text, pattern);

    if (isMatch) {
        std::cout << "Text matches the pattern!" << std::endl;
    } else {
        std::cout << "No match found!" << std::endl;
    }

    return 0;
}
  • std::regex_match() 函数接受两个参数:待匹配的字符串和正则表达式模式。
  • 如果整个字符串完全匹配正则表达式模式,则返回 true,否则返回 false
  • 此函数会尝试将整个输入字符串与模式进行匹配。如果整个字符串与模式匹配成功,则返回 true,否则返回 false

需要注意的是,std::regex_match() 会尝试对整个输入字符串进行匹配,如果要在字符串中查找匹配的子序列,可以使用 std::regex_search()

std::regex_search() 是 C++ 标准库 <regex> 中的函数,用于在给定的字符串中搜索正则表达式的匹配项。

用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("\\b\\w{5}\\b"); // 匹配长度为5的单词

    std::smatch match; // 用于存储匹配结果

    if (std::regex_search(text, match, pattern)) {
        std::cout << "Match found!" << std::endl;

        // 遍历匹配到的结果
        for (auto& m : match) {
            std::cout << "Matched substring: " << m << std::endl;
        }
    } else {
        std::cout << "No match found!" << std::endl;
    }

    return 0;
}
  • std::regex_search() 函数接受三个参数:待搜索的字符串、存储匹配结果的容器(例如 std::smatch)和正则表达式模式。
  • 它在给定的字符串中搜索匹配正则表达式模式的第一个子序列。
  • 如果找到了匹配项,则返回 true,并将匹配结果存储在提供的容器中,否则返回 false

该函数会在字符串中搜索第一个匹配项并将其存储在给定的容器中,方便进一步处理匹配到的子序列。如果需要查找所有匹配项,可以使用 std::regex_iterator 进行迭代搜索。

std::regex_replace()

std::regex_replace() 是 C++ 标准库 <regex> 中的函数,用于使用正则表达式模式对字符串进行替换操作。

用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("\\b\\w{4}\\b"); // 匹配长度为4的单词

    std::string replacedText = std::regex_replace(text, pattern, "REPLACEMENT");

    std::cout << "Original text: " << text << std::endl;
    std::cout << "Replaced text: " << replacedText << std::endl;

    return 0;
}
  • std::regex_replace() 函数接受三个参数:待替换的字符串、正则表达式模式和替换的文本。
  • 它会搜索字符串中与正则表达式模式匹配的部分,并用指定的文本进行替换。
  • 替换后的新字符串被返回,原始字符串不会被修改。

在示例中,\\b\\w{4}\\b 匹配长度为 4 的单词,然后将匹配到的单词替换为字符串 "REPLACEMENT"。注意,这里使用了双反斜杠来转义正则表达式中的特殊字符。

std::regex_constants

std::regex_constants 是 C++ 标准库 <regex> 中定义的命名空间,提供了一些常量,用于设置正则表达式的标志和选项。

主要的常量:

  1. std::regex_constants::icase:忽略大小写进行匹配。
  2. std::regex_constants::nosubs:不保存子匹配结果。
  3. std::regex_constants::optimize:优化正则表达式,提高匹配性能。
  4. std::regex_constants::collate:使用与语言相关的排序规则进行匹配。

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog";
    std::regex pattern("quick", std::regex_constants::icase); // 使用忽略大小写进行匹配

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

    return 0;
}

在上述示例中,通过指定 std::regex_constants::icase 标志,创建的正则表达式模式将忽略大小写进行匹配。使用这些常量可以在创建正则表达式对象时设定不同的匹配选项,以满足特定的匹配需求。

std::regex_constants::icase

std::regex_constants::icase 是 C++ 标准库 <regex> 中的一个常量,用于在创建正则表达式对象时设置忽略大小写的匹配模式。

当使用 std::regex_constants::icase 常量作为 std::regex 构造函数的第二个参数时,表示创建的正则表达式对象将以不区分大小写的方式进行匹配。

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The Quick Brown Fox";
    std::regex pattern("quick", std::regex_constants::icase); // 使用忽略大小写进行匹配

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

    return 0;
}

在上述示例中,正则表达式模式 "quick" 在文本中找不到精确的匹配项,但由于使用了 std::regex_constants::icase,它会忽略大小写进行查找,因此可以找到文本中的部分匹配项。

std::regex_constants::nosubs

std::regex_constants::nosubs 是 C++ 标准库 <regex> 中的一个常量,用于在创建正则表达式对象时设置,表示不保存子匹配结果。

当使用 std::regex_constants::nosubs 常量作为 std::regex 构造函数的第二个参数时,表示创建的正则表达式对象将不保存子匹配的结果。

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The Quick Brown Fox";
    std::regex pattern("(\\w+)\\s(\\w+)", std::regex_constants::nosubs); // 不保存子匹配结果

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

    return 0;
}

在上述示例中,正则表达式模式 "(\\w+)\\s(\\w+)" 匹配两个单词之间的空格,并使用 std::regex_constants::nosubs 标志创建了正则表达式对象。由于使用了 nosubs,正则表达式对象将不保存子匹配的结果,因此在搜索时只返回是否有整个模式的匹配,而不会保存子表达式的匹配结果。

std::regex_constants::optimize

std::regex_constants::optimize 是 C++ 标准库 <regex> 中的一个常量,用于在创建正则表达式对象时设置,表示优化正则表达式以提高匹配性能。

当使用 std::regex_constants::optimize 常量作为 std::regex 构造函数的第二个参数时,表示创建的正则表达式对象将进行优化,以提高匹配性能。

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The Quick Brown Fox";
    std::regex pattern("[a-z]+", std::regex_constants::optimize); // 优化正则表达式以提高匹配性能

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

    return 0;
}

在上述示例中,正则表达式模式 "[a-z]+" 用于匹配小写字母序列。通过使用 std::regex_constants::optimize 常量作为参数创建正则表达式对象,正则表达式将进行优化,提高匹配性能,特别是对于复杂的模式可以提升匹配速度。

std::regex_constants::collate

std::regex_constants::collate 是 C++ 标准库 <regex> 中的一个常量,用于在创建正则表达式对象时设置,表示使用与语言相关的排序规则进行匹配。

当使用 std::regex_constants::collate 常量作为 std::regex 构造函数的第二个参数时,表示创建的正则表达式对象将使用与语言相关的排序规则进行匹配。

用法示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string text = "The Quick Brown Fox";
    std::regex pattern("quick", std::regex_constants::icase | std::regex_constants::collate); // 使用忽略大小写和与语言相关的排序规则进行匹配

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

    return 0;
}

在上述示例中,正则表达式模式 "quick" 使用了 std::regex_constants::icase 进行忽略大小写匹配,并且使用了 std::regex_constants::collate,表示使用与语言相关的排序规则进行匹配。这允许正则表达式引擎根据当前环境的语言规则对字符进行排序和匹配。