Edit8: I've posted the solution first for anyone who might come along after me with the same problem.
Solution:
Assigned regex with = instead of invoking the () operator. Worked fine. That was stupid.
#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;
}
Original Problem:
I've been fighting with xpressive for a while now, and I've yet to make anything work. With the following code:
#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;
}
I'm not finding the match I expect. Help would be greatly appreciated.
Edit: Tried changing the regex compile line to
rex(boost::xpressive::sregex::compile(rstr, boost::xpressive::regex_constants::ECMAScript));
Still nothing.
Edit2: Compiling with MinGW GCC 4.7
Edit3: I also tried changing the line where the regex string is declared to both
std::string rstr = ".*";
and
std::string rstr = "(.*)";
Still nothing.
Edit4: I've not got the following, still with no results:
#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: I'm expecting two matches at this point, the "foo" at both the beginning and end of str.
Edit6: Tried running regex_search with the match_continuous flag set hoping I could at least get it to pick up the prefix. No dice. Also tried compiling with ECMAScript flag and running regex_search with both match_default and match_continuous flags.
Edit7: I know strstr() will work here. That's because this is a simple sample case. Boost is imperative in the actual application.
#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;
}
Prints "Match found."
for me. If you want to find all the matches ...
#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;
}
For me, this prints:
Match found at offset 0 Match found at offset 6