我正在寻找一个用于C++的正则表达式(或其他)库,它允许我指定许多模式,在字符串上运行,并返回所有模式的匹配位置。
例如: Patterns {"abcd","abcd"} String {“abce For For”}
结果:Result匹配: 0-3,11-14 abce匹配: 5-9
有人知道这样的图书馆吗?
发布于 2009-07-27 10:30:00
我推荐boost::xpressive http://www.boost.org/doc/libs/1_39_0/doc/html/xpressive.html。
一种可能的解决方案是:
string text = "abcd abce abcd";
static const sregex abcd = as_xpr("abcd"); // static - faster
sregex abce = sregex::compile( "abce" ) // compiled
sregex all = *(keep(abcd) | keep(abce));
smatch what;
if( regex_match( text, what, all ) )
{
smatch::nested_results_type::const_iterator begin = what.nested_results().begin();
smatch::nested_results_type::const_iterator end = what.nested_results().end();
for(;it != end; it++)
{
if(it->regex_id() == abcd.regex_id())
{
// you match abcd
// use it->begin() and it->end()
// or it->position() and it->length()
continue;
}
if(it->regex_id() == abce.regex_id())
{
// you match abcd...
continue;
};
}我认为这不是最好的解决方案,你可以在文档中检查“语义操作和用户定义的断言”。
发布于 2009-07-27 10:34:58
正则表达式是标准扩展tr1的一部分,并在许多标准库(如dinkumware)中实现。
我认为自己编写周围的代码是非常简单的。
发布于 2009-07-27 10:42:01
它不适用于简单的或吗
"abcd|abcd"它是一个有效的正则表达式。
https://stackoverflow.com/questions/1187315
复制相似问题