std::regex 您所在的位置:网站首页 c11正则表达式 std::regex

std::regex

2023-10-24 12:19| 来源: 网络整理| 查看: 265

  C++ 语言 标准库头文件 自立与有宿主实现 具名要求 语言支持库 概念库 (C++20) 诊断库 工具库 字符串库 容器库 迭代器库 范围库 (C++20) 算法库 数值库 本地化库 输入/输出库 文件系统库 (C++17) 正则表达式库 (C++11) 原子操作库 (C++11) 线程支持库 (C++11) 技术规范  正则表达式库 类 basic_regex(C++11) sub_match(C++11) match_results(C++11) 算法 regex_match(C++11) regex_search(C++11) regex_replace(C++11) 迭代器 regex_iterator(C++11) regex_token_iterator(C++11) 异常 regex_error(C++11) 特性 regex_traits(C++11) 常量 syntax_option_type(C++11) match_flag_type(C++11) error_type(C++11) 正则表达式文法 Modified ECMAScript-262(C++11)   定义于头文件 template bool regex_search( BidirIt first, BidirIt last,                    std::match_results& m,                    const std::basic_regex& e,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ); (1) (C++11 起) template

bool regex_search( const CharT* str,                    std::match_results& m,                    const std::basic_regex& e,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ); (2) (C++11 起) template bool regex_search( const std::basic_string& s,                    std::match_results& m,                    const std::basic_regex& e,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ); (3) (C++11 起) template bool regex_search( BidirIt first, BidirIt last,                    const std::basic_regex& e,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ); (4) (C++11 起) template

bool regex_search( const CharT* str,                    const std::basic_regex& e,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ); (5) (C++11 起) template bool regex_search( const std::basic_string& s,                    const std::basic_regex& e,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ); (6) (C++11 起) template bool regex_search( const std::basic_string&&,                    std::match_results&,                    const std::basic_regex&,                    std::regex_constants::match_flag_type flags =

                       std::regex_constants::match_default ) = delete; (7) (C++14 起)

确定正则表达式 e 和目标字符序列中的某个子序列间是否有匹配。

1) 分析泛型范围 [first,last) 。返回匹配结果于 m 。 2) 分析 str 所指向的空终止字符串。返回匹配结果于 m 。 3) 分析 string s 。返回匹配结果于 m 。 4-6) 等价于 (1-3) ,只是省略匹配结果。 7) 禁止重载 3 接受临时字符串,否则此函数将以立即变为非法的 string 迭代器填充 match_results m 。

regex_search 将成功地匹配给定序列的任何子序列,相对地 std::regex_match 仅若正则表达式匹配整个序列才返回 true 。

参数 first, last - 标识目标字符序列的范围 str - 指向空终止字符序列的指针 s - 标识目标字符序列的指针 e - 应当应用到目标字符序列的 std::regex m - 匹配结果 flags - 掌管搜索行为的 std::regex_constants::match_flag_type 类型要求 -BidirIt 必须满足遗留双向迭代器 (LegacyBidirectionalIterator) 的要求。 -Alloc 必须满足分配器 (Allocator) 的要求。 返回值

若匹配存在则返回 true ,否则返回 false 。任一情况下,更新对象 m 如下:

若匹配不存在:

m.ready() == true m.empty() == true m.size() == 0

若匹配存在:

m.ready() true m.empty() false m.size() 有标记子表达式的数量加 1 ,即 1+e.mark_count() m.prefix().first first m.prefix().second m[0].first m.prefix().matched m.prefix().first != m.prefix().second m.suffix().first m[0].second m.suffix().second last m.suffix().matched m.suffix().first != m.suffix().second m[0].first 匹配序列的起始 m[0].second 匹配数列的结尾 m[0].matched true m[n].first 匹配有标记子表达式 n 的序列的起始,或若子表达式不参与匹配则为 last m[n].second 匹配有标记子表达式 n 的序列的结尾,或若子表达式不参与匹配则为 last m[n].matched 若子表达式 n 参与匹配则为 true ,否则为 false 注意

为在目标序列内检验所有匹配,可在循环中调用 std::regex_search ,每次从先前调用的 m[0].second 重新开始。 std::regex_iterator 提供对此迭代的简易接口。

示例 运行此代码 #include #include #include   int main() { std::string lines[] = {"Roses are #ff0000", "violets are #0000ff", "all of my base are belong to you"};   std::regex color_regex("#([a-f0-9]{2})" "([a-f0-9]{2})" "([a-f0-9]{2})");   // 简单匹配 for (const auto &line : lines) { std::cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有