【问题标题】:Boost Regex Giving Blank CaptureBoost Regex 提供空白捕获
【发布时间】:2014-03-12 11:04:32
【问题描述】:

我正在尝试使用 boost regex 库匹配 XXX >|=| 形式的字符串。我下面的代码显示我正在尝试匹配“x.y

boost::regex e("(.+?) *(>=|<=|==|>|<) *(.+)");
boost::smatch what;
if (boost::regex_match(std::string("x.y<123"), what, e)) {
    for (int i = 0; i < what.size(); ++ i)
        std::cout << std::string(what[i]) << std::endl;
}
else
    std::cout << "Fail matching" << std::endl;

【问题讨论】:

  • 您的正则表达式看起来不错,但您是否使用了匹配函数的最后一个参数match_flag_type 默认行为可以解释为什么您没有得到“
  • @alexbuisson:我使用默认行为获得了相同的结果。

标签: c++ regex boost


【解决方案1】:

匹配结果引用回被搜索的字符串。由于你的是临时的,结果是不确定的。

if (boost::regex_match(std::string("x.y<123"), what, e)) { // bad


std::string s("x.y<123");
if (boost::regex_match(s, what, e)) { // good

【讨论】:

  • +1,胜过元字符的阴谋 :)
  • 不错,没想到!非常感谢。
  • @CasimiretHippolyte 我讨厌正则表达式语法。永远不要记住 (\( 在本月的味道中是什么意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 2013-07-26
  • 2012-05-13
  • 2017-09-19
  • 1970-01-01
相关资源
最近更新 更多