【问题标题】:boost::regex with case-insensitive match with UTF-8 (e.g. uppercase versus lowercase umlauts)boost::regex 与 UTF-8 不区分大小写匹配(例如,大写与小写变音符号)
【发布时间】:2013-04-01 03:08:21
【问题描述】:

在构建具有国际 Unicode 组件 (ICU) 支持的 boost::regex 版本 1.52 库后,具有不区分大小写匹配的正则表达式似乎无法按预期处理大写和小写的德语变音符号。

static const std::string pattern("^.*" "\303\226" ".*$");
static const std::string   test1("SCH" "\303\226" "NE");
static const std::string   test2("sch" "\303\266" "ne");
static const boost::regex exp(pattern, boost::regex::icase);
const char *result = (boost::regex_match(test1, exp)) ? "Match" : "NoMatch";
std::cout << "Testing \"" << test1 << "\" against pattern \"" << pattern 
    << "\" : " << result << std::endl;
result = (boost::regex_match(test2, exp)) ? "Match" : "NoMatch";
std::cout << "Testing \"" << test2 << "\" against pattern \"" << pattern 
    << "\" : " << result << std::endl;

产量:

Testing "SCHÖNE" against pattern "^.*Ö.*$" : Match
Testing "schöne" against pattern "^.*Ö.*$" : NoMatch

【问题讨论】:

    标签: c++ regex boost utf-8


    【解决方案1】:

    Working with Unicode and ICU string types.

    Example on LWS.

    #include <iostream>
    #include <boost/regex.hpp>
    #include <boost/regex/icu.hpp>
    int main()
    {
       static const std::string pattern("^.*" "\303\226" ".*$");
       static const std::string   test1("SCH" "\303\226" "NE");
       static const std::string   test2("sch" "\303\266" "ne");
       static const boost::u32regex exp=boost::make_u32regex(pattern, boost::regex::icase);
       const char *result = (boost::u32regex_match(test1, exp)) ? "Match" : "NoMatch";
       std::cout << "Testing \"" << test1 << "\" against pattern \"" << pattern 
          << "\" : " << result << std::endl;
       result = (boost::u32regex_match(test2, exp)) ? "Match" : "NoMatch";
       std::cout << "Testing \"" << test2 << "\" against pattern \"" << pattern 
          << "\" : " << result << std::endl;
    }
    

    【讨论】:

    • 非常感谢。我很感激。一个小问题是我的环境似乎无法编译您的解决方案。我在“u_tolower_49”中得到了多个未定义的引用,但我会处理它。非常感谢。
    猜你喜欢
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2015-06-13
    • 1970-01-01
    • 2014-09-07
    相关资源
    最近更新 更多