Boost 从电报机器人更新中删除表情

Boost 从电报机器人更新中删除表情,boost,unicode,c++03,boost-regex,boost-propertytree,Boost,Unicode,C++03,Boost Regex,Boost Propertytree,我想从用boost属性树解析的json电报机器人更新中删除emojis 我尝试从这个答案和其他几个方法中使用正则表达式模式,但是我不知道如何让它们在C++中工作(下面会导致崩溃): BOOST\u FOREACH(const BOOST::property\u tree::ptree::value\u type&child,jsontree.get\u child(“结果”)) { 字符串消息(child.second.get(“message.text”).c_str()); boost::

我想从用boost属性树解析的json电报机器人更新中删除emojis

我尝试从这个答案和其他几个方法中使用正则表达式模式,但是我不知道如何让它们在C++中工作(下面会导致崩溃):

BOOST\u FOREACH(const BOOST::property\u tree::ptree::value\u type&child,jsontree.get\u child(“结果”))
{
字符串消息(child.second.get(“message.text”).c_str());
boost::regex exp(“/[\u{1F600}-\u{1F6FF}]/”;
std::string message_clean=regex_replace(message,exp,“”);
...
}
在中的0x00007FF87C1C7788处引发异常 CrysisWarsDedicatedServer.exe:微软C++异常: 内存位置0x000000001003F138处的boost::exception_detail::clone_impl>。CrysisWarsDedicatedServer.exe 0x000 07FF08C1C788未处理异常:微软C++ 例外情况: 内存位置0x000000001003F138处的boost::exception_detail::clone_impl>


第一个问题是对具有任意文本编码的字节数组使用
.c_str()
。没有必要,所以不要这样做

下一步,<代码> \ u '/COD>不是一个有效的C++字符转义。你是说

'\\u'

最后,确保Boost正则表达式是正确的,并使用适当的函数

在花了一些时间处理这些文档页面之后

我想出了

//#define BOOST_HAS_ICU
#include <boost/property_tree/json_parser.hpp>
#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
#include <iostream>
std::string asUtf8(icu::UnicodeString const& ustr);

std::string sample = R"(
{
    "message":{
       "message_id":123,
       "from":{
          "id":12345,
          "first_name":"name",
          "username":"username"
       },
       "chat":{
          "id":12345,
          "first_name":"name",
          "username":"username",
          "type":"private"
       },
       "date":1478144459,
       "text":"this is \ud83d\udca9 a sentence"
    }
}
)";

int main() {

    boost::property_tree::ptree pt;
    {
        std::istringstream iss(sample);
        read_json(iss, pt);
    }
    auto umessage       = icu::UnicodeString::fromUTF8(pt.get("message.text", ""));
    boost::u32regex exp = boost::make_u32regex("\\p{So}");

    auto clean = boost::u32regex_replace(umessage, exp, UnicodeString::fromUTF8("<symbol>"));

    std::cout << asUtf8(clean) << "\n";
}

std::string asUtf8(icu::UnicodeString const& ustr) {
    std::string r;
    {
        icu::StringByteSink<std::string> bs(&r);
        ustr.toUTF8(bs);
    }

    return r;
}
//\define BOOST\u已经
#包括
#包括
#包括
#包括
std::字符串asUtf8(icu::UnicodeString const和ustr);
std::string sample=R“(
{
“信息”:{
“消息id”:123,
“发件人”:{
“id”:12345,
“名字”:“名字”,
“用户名”:“用户名”
},
“聊天”:{
“id”:12345,
“名字”:“名字”,
“用户名”:“用户名”,
“类型”:“专用”
},
“日期”:1478144459,
“文本”:“这是\ud83d\udca9一个句子”
}
}
)";
int main(){
boost::property_tree::ptree pt;
{
标准::istringstream iss(样本);
读json(国际空间站,邮政编码);
}
auto-umessage=icu::UnicodeString::fromUTF8(pt.get(“message.text”),“”);
boost::u32regex exp=boost::make_32regex(“\\p{So}”);
自动清除=boost::u32regex_replace(umessage,exp,UnicodeString::fromUTF8(“”);

std::cout这个问题可以使用更多的细节:我假设
regex
实际上是
boost::regex
。当你不能让它工作时,你是什么意思?它编译了吗?它在运行时崩溃了吗?或者
message\u clean
没有给你想要的结果吗(例如,所有的表情仍然在字符串中)?您是否尝试过使用更简单的正则表达式(例如,删除所有数字)为确保
regex\u replace
正常工作?您使用的是哪个版本的
boost
?抱歉;是的,boost::regex。问题是它崩溃了,版本是boost 1.55它是如何崩溃的,在哪里?尝试连接到进程的。在
BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, jsontree.get_child("result"))
{

        std::string message(child.second.get<std::string>("message.text").c_str());

        boost::regex exp("/[\u{1F600}-\u{1F6FF}]/");
        std::string message_clean = regex_replace(message, exp, "");

        ...
}
//#define BOOST_HAS_ICU
#include <boost/property_tree/json_parser.hpp>
#include <boost/regex.hpp>
#include <boost/regex/icu.hpp>
#include <iostream>
std::string asUtf8(icu::UnicodeString const& ustr);

std::string sample = R"(
{
    "message":{
       "message_id":123,
       "from":{
          "id":12345,
          "first_name":"name",
          "username":"username"
       },
       "chat":{
          "id":12345,
          "first_name":"name",
          "username":"username",
          "type":"private"
       },
       "date":1478144459,
       "text":"this is \ud83d\udca9 a sentence"
    }
}
)";

int main() {

    boost::property_tree::ptree pt;
    {
        std::istringstream iss(sample);
        read_json(iss, pt);
    }
    auto umessage       = icu::UnicodeString::fromUTF8(pt.get("message.text", ""));
    boost::u32regex exp = boost::make_u32regex("\\p{So}");

    auto clean = boost::u32regex_replace(umessage, exp, UnicodeString::fromUTF8("<symbol>"));

    std::cout << asUtf8(clean) << "\n";
}

std::string asUtf8(icu::UnicodeString const& ustr) {
    std::string r;
    {
        icu::StringByteSink<std::string> bs(&r);
        ustr.toUTF8(bs);
    }

    return r;
}
this is <symbol> a sentence