Boost logo

Boost :

From: Stephan T. Lavavej (stl_at_[hidden])
Date: 2003-12-09 21:27:26


Using Boost CVS 20031120, I observe the following strange behavior:

C:\Temp\work>type mlar.cc
#include <boost/regex.hpp>
#include <iostream>
#include <string>
using namespace boost;
using namespace std;

int main() {
    const string s = "City 15 : 25 , 776";

    cout << "Non Greedy:" << endl;
    cout << "First: \"" << regex_merge(s, regex("(.*?) : (.*?) , (.*?)"),
"$1") << "\"" << endl;
    cout << "Second: \"" << regex_merge(s, regex("(.*?) : (.*?) , (.*?)"),
"$2") << "\"" << endl;
    cout << "Third: \"" << regex_merge(s, regex("(.*?) : (.*?) , (.*?)"),
"$3") << "\"" << endl;

    cout << endl;
    cout << "Greedy:" << endl;
    cout << "First: \"" << regex_merge(s, regex("(.*) : (.*) , (.*)"),
"$1") << "\"" << endl;
    cout << "Second: \"" << regex_merge(s, regex("(.*) : (.*) , (.*)"),
"$2") << "\"" << endl;
    cout << "Third: \"" << regex_merge(s, regex("(.*) : (.*) , (.*)"),
"$3") << "\"" << endl;

    cout << endl;
    cout << "Non Greedy Except For Greedy Last:" << endl;
    cout << "First: \"" << regex_merge(s, regex("(.*?) : (.*?) , (.*)"),
"$1") << "\"" << endl;
    cout << "Second: \"" << regex_merge(s, regex("(.*?) : (.*?) , (.*)"),
"$2") << "\"" << endl;
    cout << "Third: \"" << regex_merge(s, regex("(.*?) : (.*?) , (.*)"),
"$3") << "\"" << endl;

}

C:\Temp\work>gpp -Wall -W mlar.cc -o mlar.exe -lboost_regex

C:\Temp\work>mlar
Non Greedy:
First: "City 15776"
Second: "25776"
Third: "776"

Greedy:
First: "City 15"
Second: "25"
Third: "776"

Non Greedy Except For Greedy Last:
First: "City 15"
Second: "25"
Third: "776"

C:\Temp\work>

According to my understanding of regular expressions, all three should
report the same things. But the Non Greedy example produces mangled output.
I can't even understand how that output is being generated.

Is this a Boost bug, or have I done something wrong?

Stephan T. Lavavej
http://nuwen.net


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk