gpt4 book ai didi

c++ - 无法与 boost xpressive 动态正则表达式匹配

转载 作者:搜寻专家 更新时间:2023-10-31 00:41:28 26 4
gpt4 key购买 nike

Edit8:我已经首先为可能遇到相同问题的人发布了解决方案。

解决方案:

使用 = 分配正则表达式,而不是调用 () 运算符。工作正常。那是愚蠢的。

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive_dynamic.hpp>

int main()
{
std::string str = "foobarfoo";
boost::xpressive::sregex rex;
std::string rstr = "foo";
rex = boost::xpressive::sregex::compile(rstr, boost::xpressive::regex_constants::ECMAScript);
if (boost::xpressive::regex_search(str, rex, boost::xpressive::regex_constants::match_continuous))
std::cout << "Match found.";
else
std::cout << "No match found.";
return 0;
}

原始问题:

我已经与 xpressive 斗争了一段时间,但我还没有做出任何工作。使用以下代码:

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive_dynamic.hpp>

int main()
{
std::string str = "foobar";
boost::xpressive::sregex rex;
std::string rstr = "foo";
rex(boost::xpressive::sregex::compile(rstr));
if (boost::xpressive::regex_match(str, rex))
std::cout << "Match found.";
else
std::cout << "No match found.";
return 0;
}

我没有找到我期望的匹配项。将不胜感激。

编辑:尝试将正则表达式编译行更改为

rex(boost::xpressive::sregex::compile(rstr, boost::xpressive::regex_constants::ECMAScript));

仍然没有。

Edit2:使用 MinGW GCC 4.7 编译

Edit3:我还尝试更改声明正则表达式字符串的行

std::string rstr = ".*";

std::string rstr = "(.*)";

仍然没有。

Edit4:我没有得到以下信息,仍然没有结果:

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive_dynamic.hpp>

int main()
{
std::string str = "foobarfoo";
boost::xpressive::sregex rex;
std::string rstr = "(foo)";
rex(boost::xpressive::sregex::compile(rstr));//;, boost::xpressive::regex_constants::ECMAScript));
if (boost::xpressive::regex_search(str, rex, boost::xpressive::regex_constants::match_default))
std::cout << "Match found.";
else
std::cout << "No match found.";
return 0;
}

Edit5:此时我期待两个匹配项,str 开头和结尾处的“foo”。

Edit6: 尝试在设置了 match_continuous 标志的情况下运行 regex_search,希望我至少可以让它获取前缀。没有骰子。还尝试使用 ECMAScript 标志进行编译并使用 match_default 和 match_continuous 标志运行 regex_search。

Edit7:我知道 strstr() 可以在这里工作。那是因为这是一个简单的示例案例。 Boost在实际应用中是必不可少的。

最佳答案

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive_dynamic.hpp>

int main()
{
std::string str = "foobar";
std::string rstr = "foo";
boost::xpressive::sregex rex = boost::xpressive::sregex::compile(rstr);
if (boost::xpressive::regex_search(str, rex))
std::cout << "Match found." << std::endl;
else
std::cout << "No match found." << std::endl;
}

为我打印“找到匹配项。”。如果你想找到所有的匹配......

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive_dynamic.hpp>

int main()
{
std::string str = "foobarfoo";
std::string rstr = "foo";
boost::xpressive::sregex rex = boost::xpressive::sregex::compile(rstr);
boost::xpressive::sregex_iterator it(str.begin(), str.end(), rex), end;

for (; it != end; ++it )
std::cout << "Match found at offset "
<< ((*it)[0].first - str.begin())
<< std::endl;
}

对我来说,这会打印:

Match found at offset 0Match found at offset 6

关于c++ - 无法与 boost xpressive 动态正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com