提升正则表达式排除一个字符
Boost Regex Exclude one character
我正在锁定一个 boost 正则表达式,它将排除所有包含字符 |.
的字符串
- “1234|”应该排除
- "|eee" 应该排除
- 不应排除“...ff”。
我无法更改上面的代码来删除 boost::regex_match 的结果的否定。
if ( !boost::regex_match( sValue, boost::regex("[^\|]") ) )
{
// string contains character |
}
else
{
// string doesn't contains character |
}
为什么正则表达式[^\|]不符合我的需求?
最简单的做法是在正则表达式 "\|"
匹配时排除。 (效率也更高)。
此外,您显然不需要正则表达式:
bool exclude = (std::string::npos != s.find('|'));
我终于找到了正确的正则表达式:^[^|]*$
我正在锁定一个 boost 正则表达式,它将排除所有包含字符 |.
的字符串- “1234|”应该排除
- "|eee" 应该排除
- 不应排除“...ff”。
我无法更改上面的代码来删除 boost::regex_match 的结果的否定。
if ( !boost::regex_match( sValue, boost::regex("[^\|]") ) )
{
// string contains character |
}
else
{
// string doesn't contains character |
}
为什么正则表达式[^\|]不符合我的需求?
最简单的做法是在正则表达式 "\|"
匹配时排除。 (效率也更高)。
此外,您显然不需要正则表达式:
bool exclude = (std::string::npos != s.find('|'));
我终于找到了正确的正则表达式:^[^|]*$