【BOOST C++ String专题08】Boost.Regex

目录

一、Boost.Regex提要

二、示例 8.1。使用 boost::regex_match() 比较字符串

三、示例 8.2。使用 boost::regex_search() 搜索字符串

四、示例 8.3。用 boost::regex_replace() 替换字符串中的字符

五、示例 8.4。使用正则表达式中的组引用格式化

六、示例 8.5。格式标志

七、示例 8.6。使用 boost::regex_token_iterator 迭代字符串

八、示例 8.7。使用 boost::regex_token_iterator 访问组

九、示例 8.8。将语言环境链接到正则表达式


一、Boost.Regex提要

        Boost.Regex 允许您在 C++ 中使用正则表达式。由于该库是自 C++11 以来标准库的一部分,因此如果您的开发环境支持 C++11,则无需依赖 Boost.Regex。如果包含头文件 regex,则可以在命名空间 std 中使用同名的类和函数。

        Boost.Regex 中最重要的两个类是 boost::regex 和 boost::smatch,它们都在 boost/regex.hpp 中定义。前者定义正则表达式,后者保存搜索结果。

        Boost.Regex 提供了三种不同的函数来搜索正则表达式。

二、示例 8.1。使用 boost::regex_match() 比较字符串

#include 
#include 
#include int main()
{std::string s = "Boost Libraries";boost::regex expr{"\\w+\\s\\w+"};std::cout << std::boolalpha << boost::regex_match(s, expr) << '\n';
}

        boost::regex_match()(参见示例 8.1)将字符串与正则表达式进行比较。仅当表达式与完整字符串匹配时,它才会返回 true。

        boost::regex_search() 在字符串中搜索正则表达式。

三、示例 8.2。使用 boost::regex_search() 搜索字符串

#include 
#include 
#include int main()
{std::string s = "Boost Libraries";boost::regex expr{"(\\w+)\\s(\\w+)"};boost::smatch what;if (boost::regex_search(s, what, expr)){std::cout << what[0] << '\n';std::cout << what[1] << "_" << what[2] << '\n';}
}

        boost::regex_search() 期望对 boost::smatch 类型的对象的引用作为附加参数,用于存储结果。 boost::regex_search() 仅搜索组。这就是示例 8.2 根据正则表达式中的两个组返回两个字符串的原因。

        结果存储类 boost::smatch 是一个包含 boost::sub_match 类型元素的容器,可以通过类似于 std::vector 的接口访问。例如,可以通过 operator[] 访问元素。

        boost::sub_match 类将迭代器存储到与正则表达式的组相对应的字符串中的特定位置。因为 boost::sub_match 是从 std::pair 派生的,所以可以使用 first 和 second 访问引用特定子字符串的迭代器。但是,要将子字符串写入标准输出流,您不必访问这些迭代器(参见示例 8.2)。使用重载运算符 operator<<,可以将子字符串直接写入标准输出。

        请注意,因为迭代器用于指向匹配的字符串,所以 boost::sub_match 不会复制它们。这意味着只有当迭代器引用的相应字符串存在时,才能访问结果。

        此外,请注意容器 boost::smatch 的第一个元素存储迭代器,该迭代器引用与整个正则表达式匹配的字符串。与第一组匹配的第一个子字符串可在索引 1 处访问。

        Boost.Regex 提供的第三个函数是 boost::regex_replace()(参见示例 8.3)。

四、示例 8.3。用 boost::regex_replace() 替换字符串中的字符

#include 
#include 
#include int main()
{std::string s = " Boost Libraries ";boost::regex expr{"\\s"};std::string fmt{"_"};std::cout << boost::regex_replace(s, expr, fmt) << '\n';
}

        除了搜索字符串和正则表达式之外,boost::regex_replace() 需要一种格式来定义如何替换与正则表达式的各个组匹配的子字符串。如果正则表达式不包含任何组,则使用给定格式一对一替换相应的子字符串。因此,示例 8.3 将输出 _Boost_Libraries_。

        boost::regex_replace() 总是在整个字符串中搜索正则表达式。因此,程序实际上用下划线替换了所有三个空格。

五、示例 8.4。使用正则表达式中的组引用格式化

#include 
#include 
#include int main()
{std::string s = "Boost Libraries";boost::regex expr{"(\\w+)\\s(\\w+)"};std::string fmt{"\\2 \\1"};std::cout << boost::regex_replace(s, expr, fmt) << '\n';
}

        该格式可以访问由正则表达式组返回的子字符串。示例 8.4 使用此技术交换第一个和最后一个单词,从而显示 Libraries Boost。

        正则表达式和格式有不同的标准。这三个函数中的每一个都带有一个附加参数,允许您选择特定的标准。您还可以指定是否应以特定格式解释特殊字符,或者该格式是否应替换与正则表达式匹配的完整字符串。

六、示例 8.5。格式标志

#include 
#include 
#include int main()
{std::string s = "Boost Libraries";boost::regex expr{"(\\w+)\\s(\\w+)"};std::string fmt{"\\2 \\1"};std::cout << boost::regex_replace(s, expr, fmt,boost::regex_constants::format_literal) << '\n';
}

        示例 8.5 将标志 boost::regex_constants::format_literal 作为第四个参数传递给 boost::regex_replace() 以抑制对格式中特殊字符的处理。因为匹配正则表达式的完整字符串被替换为格式,所以示例 8.5 的输出为 \2 \1。

七、示例 8.6。使用 boost::regex_token_iterator 迭代字符串

#include 
#include 
#include int main()
{std::string s = "Boost Libraries";boost::regex expr{"\\w+"};boost::regex_token_iterator it{s.begin(), s.end(),expr};boost::regex_token_iterator end;while (it != end)std::cout << *it++ << '\n';
}

        借助 boost::regex_token_iterator,Boost.Regex 提供了一个类来使用正则表达式迭代字符串。在示例 8.6 中,迭代返回 s 中的两个单词。它使用迭代器初始化为 s 和正则表达式“\w+”。默认构造函数创建一个结束迭代器。

        示例 8.6 显示 Boost 和库。

八、示例 8.7。使用 boost::regex_token_iterator 访问组

#include 
#include 
#include int main()
{std::string s = "Boost Libraries";boost::regex expr{"(\\w)\\w+"};boost::regex_token_iterator it{s.begin(), s.end(),expr, 1};boost::regex_token_iterator end;while (it != end)std::cout << *it++ << '\n';
}

        您可以将数字作为附加参数传递给 boost::regex_token_iterator 的构造函数。如果传递了 1,如示例 8.7 中所示,迭代器将返回正则表达式中的第一个组。因为使用了正则表达式“(\w)\w+”,所以示例 8.7 将首字母 B 和 L 写入标准输出。

        如果将 -1 传递给 boost::regex_token_iterator,则正则表达式是分隔符。用 -1 初始化的迭代器返回与正则表达式不匹配的子字符串。

九、示例 8.8。将语言环境链接到正则表达式

#include 
#include 
#include 
#include int main()
{std::string s = "Boost k\xfct\xfcphaneleri";boost::basic_regex> expr;expr.imbue(std::locale{"Turkish"});expr = "\\w+\\s\\w+";std::cout << std::boolalpha << boost::regex_match(s, expr) << '\n';
}

Example 8.8

        示例 8.8 将带有 imbue() 的语言环境链接到 expr。这样做是为了将正则表达式应用于字符串“Boost kütüphaneleri”,这是“Boost Libraries”的土耳其语翻译。如果变音符号应被解析为有效字母,则必须设置语言环境 - 否则 boost::regex_match() 返回 false。

        要使用 std::locale 类型的语言环境,expr 必须基于使用 boost::cpp_regex_traits 类型实例化的类。这就是示例 8.8 不使用 boost::regex 而是使用 boost::basic_regex> 的原因。使用 boost::basic_regex 的第二个模板参数,可以间接定义 imbue() 的参数。只有使用 boost::cpp_regex_traits 才能将 std::locale 类型的语言环境传递给 imbue()。

注意
        如果您想在 POSIX 操作系统上运行该示例,请将“Turkish”替换为“tr_TR”。还要确保安装了土耳其语的语言环境。

        请注意,boost::regex 是使用依赖于平台的第二个模板参数定义的。在 Windows 上,此参数是 boost::w32_regex_traits,它允许将 LCID 传递给 imbue()。 LCID 是在 Windows 上标识某种语言和文化的数字。如果您想编写独立于平台的代码,则必须显式使用 boost::cpp_regex_traits,如示例 8.8 中所示。或者,您可以定义宏 BOOST_REGEX_USE_CPP_LOCALE。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部