【问题标题】:Boost Regex Exclude one characterBoost Regex 排除一个字符
【发布时间】:2015-05-25 22:06:16
【问题描述】:

我正在锁定一个 boost 正则表达式,它将排除所有包含字符 | 的字符串。

  • “1234|”应该排除
  • “|eee”应该被排除
  • “...ff”不应被排除。

我无法更改上面的代码以删除对 boost::regex_match 结果的否定。

if ( !boost::regex_match( sValue, boost::regex("[^\|]") ) )
{
// string contains character |
}
else
{
// string doesn't contains character |
}

为什么正则表达式 [^\|] 不能满足我的需求?

【问题讨论】:

  • 不是答案,但如果没有正则表达式你也不能这样做:sValue.find('|') == std::string::npos
  • 感谢您的回答。上面的代码是框架的一部分,我需要使用正则表达式,我不能修改代码。

标签: c++ regex boost regex-negation


【解决方案1】:

当正则表达式"\|" 确实匹配时,最简单的做法是排除。 (也更有效率)。

此外,您显然不需要正则表达式:

bool exclude = (std::string::npos != s.find('|'));

【讨论】:

    【解决方案2】:

    我终于找到了正确的正则表达式:^[^|]*$

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2020-02-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多