2

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.

Logan Jones
  • 324
  • 1
  • 3
  • 10
  • Two of them. A match for "foo" at both the beginning and end of "foobarfoo" – Logan Jones Oct 31 '12 at 02:00
  • For that you are going to need a regex_iterator. Does it have to be boost? You could make your life easy and just use strstr(). – imreal Oct 31 '12 at 02:05
  • In this specific example strstr() would work. I've purposely made this a very simple case for illustration purposes. It has to be boost in the actual project, though. – Logan Jones Oct 31 '12 at 02:08

2 Answers2

3
#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
Eric Niebler
  • 5,927
  • 2
  • 29
  • 43
  • It works for me too. That's why I posted it up top as the solution. – Logan Jones Oct 31 '12 at 16:19
  • 1
    For future reference, rather than editing your question with the answer, the typical way this is handled on StackOverflow is to "accept" an answer. Only the original poster can accept an answer, and doing so makes it simple for other people to find it. Glad to help out. – Eric Niebler Oct 31 '12 at 18:24
  • I'm well aware. I also accepted an answer. The problem is that your post actually came after I had accepted an answer, and the solution was provided in the comments under an answer that didn't actually provide the solution. As such, I edited the original post to illustrate the solution rather than having somepony come along and become more confused when the solution is actually not in the accepted answer but in its comments. – Logan Jones Nov 02 '12 at 14:37
2

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 use regex_search with the flag match_continuous set.

http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/ref/regex_match.html

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • Switched to regex_search to no avail. Don't necessarily only want the prefix, so I called it with the match_default flag instead. Still nothing. Updated parent post to reflect (lack of) progress. – Logan Jones Oct 31 '12 at 02:01
  • Also tried with match_continuous flag because at this point getting the prefix would feel like making progress. Still no matches. – Logan Jones Oct 31 '12 at 02:03
  • 1
    `rex(boost::xpressive::sregex::compile(rstr));` invoking operator () on a sregex does what exactly? Try either constructing the rex directly, or using operator=... – Yakk - Adam Nevraumont Oct 31 '12 at 02:11
  • 1
    I'd hug you if that was at all possible. Or buy you a beer, if you'd prefer. – Logan Jones Oct 31 '12 at 02:19
  • 1
    Directly assigning a string to an xpressive regex isn't allowed because of ambiguity between the static and dynamic regex dialects. In `rex = "xyz";`, xpressive needs to know whether "xyz" should be interpreted as a string literal or as a pattern. Rather an guessing, xpressive forces you to be explicit. – Eric Niebler Oct 31 '12 at 05:44