【问题标题】:Strip emojis from Telegram bot update从 Telegram 机器人更新中去除表情符号
【发布时间】:2017-03-16 13:48:19
【问题描述】:

我想从使用 boost 属性树解析的 json Telegram 机器人更新中删除表情符号

我尝试使用这个答案和其他一些答案中的正则表达式模式,但我不确定如何让它们在 C++ 中工作(以下导致崩溃): https://stackoverflow.com/a/24674266/2212021

"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"
}
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, "");

        ...
}

在 0x00007FF87C1C7788 处抛出异常 CrysisWarsDedicatedServer.exe:Microsoft C++ 异常: boost::exception_detail::clone_impl > 在内存位置 0x000000001003F138。 CrysisWarsDedicatedServer.exe 中 0x00007FF87C1C7788 处未处理的异常:Microsoft C++ 例外: boost::exception_detail::clone_impl > 在内存位置 0x000000001003F138。

【问题讨论】:

  • 这个问题可以使用更多细节:我假设regex 实际上是boost::regex。当你不能让它工作时,你是什么意思?它编译吗?运行时会崩溃吗?或者message_clean 没有给你想要的结果(例如,所有的表情符号都还在字符串中)?您是否尝试过使用更简单的正则表达式(例如删除所有数字)来确保 regex_replace 正常工作?你用的是什么版本的boost
  • 道歉;是的,提升::正则表达式。问题是它崩溃了,版本是 boost 1.55
  • 它是如何崩溃的,在哪里?
  • 尝试附加到该过程。编辑主帖

标签: boost unicode c++03 boost-regex boost-propertytree


【解决方案1】:

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

接下来,'\u' 不是有效的 C++ 字符转义。你的意思是'\\u'

最后,确保 Boost Regex 是 compiled with Unicode support 并使用适当的函数。

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

我想出了

Live On Wandbox

//#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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2018-10-20
  • 2020-10-29
  • 2019-08-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多