Regex boost::替换所有单词问题
我的正则表达式不起作用。为什么?Regex boost::替换所有单词问题,regex,boost,Regex,Boost,我的正则表达式不起作用。为什么? boost::regex re("anonuuid|anon_id", boost::regex::icase); target_string = "anonuuid final.device_anonuuid anon_id"; boost::replace_all(target_string, "anonuuid", "device_anonuuid"); 其思想是查找并替换整个单词anonuid或anonu id。我使用了单词边界标记\b,但即使使用它,
boost::regex re("anonuuid|anon_id", boost::regex::icase);
target_string = "anonuuid final.device_anonuuid anon_id";
boost::replace_all(target_string, "anonuuid", "device_anonuuid");
其思想是查找并替换整个单词anonuid或anonu id。我使用了单词边界标记\b,但即使使用它,它也不起作用。下面是我代码的结果
device_anonuuid final.device_device_anonuuid anon_id"
但我希望得到这个
device_anonuuid final.device_anonuuid device_anonuuid
提前谢谢。如果您想要
regex\u replace\u all
,请查看
另请注意:
- 您需要转义
(例如\b
)“\\b”
- 您需要将左值作为格式传递
- 还有一个
返回一个新字符串,而不是修改输入字符串regex\u replace\u all\u copy
#包括
#包括
#包括
#包括
int main()
{
boost::regex re(\\b(anonuid | anon_id)\\b),boost::regex::icase);
std::string target\u string=“anonuid final.device\u anonuid anon\u id”;
std::string format=“qqq”;
boost::replace_all_regex(target_string,re,format,boost::match_flag_type::match_default);
std::cout你在用什么正则表达式?这一个boost::regex-re(“anonuid | anon_-id”,boost::regex::icase)
我在那里没有看到单词边界匹配器。我试过这个boost::regex-re(“\banonuid\b | \banon_-id\b”,boost::regex::icase)
但结果是device\u anonuuid final.device\u device\u anonuuid anon\u id
您的代码实际上没有使用re
。您可能需要boost::regex\u replace
。文本键入(不是在代码中)regex\u replace\u all
(应该是replace\u all\u regex
)和regex\u replace\u all\u copy
为什么不只是使用<代码> ReXExthExchange < /C>(不包括代码> Boo::算法< /代码>),也不相信<代码>格式<代码>必须是一个LVC++。谢谢,TI工作。但是为什么我要逃出“B”两次?@
#include <boost/regex.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <string>
#include <iostream>
int main()
{
boost::regex re("\\b(anonuuid|anon_id)\\b", boost::regex::icase);
std::string target_string = "anonuuid final.device_anonuuid anon_id";
std::string format = "QQQQ";
boost::replace_all_regex(target_string, re, format, boost::match_flag_type::match_default);
std::cout << target_string;
}