Reputation: 324
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.
Upvotes: 2
Views: 1592
Reputation: 6177
#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
Upvotes: 3
Reputation: 275500
Try regex_search
instead of regex_match
?
The boost docs for regex_match
state:
Note that the result is true only if the expression matches the whole of the input sequence. If you want to search for an expression somewhere within the sequence then use
regex_search
. If you want to match a prefix of the character string then useregex_search
with the flagmatch_continuous
set.
http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/ref/regex_match.html
Upvotes: 2