Boost logo

Boost Users :

From: Darin Adler (darin_at_[hidden])
Date: 2002-06-30 00:26:14


On Saturday, June 29, 2002, at 11:29 AM, soleani wrote:

> boost::cmatch what;
> if ( boost::regex_search( input, what, regexRedirectedSection,
> boost::regbase::perl ) )
> {
> cerr << "Matched new style section redirection!!!! Input
> is: " << input << endl;
> cerr << "0 gives: --" << what[0].first << "-- and second: "
> << what[0].second << endl;
> cerr << "1 gives: --" << what[1].first << "-- and second: "
> << what[1].second << endl;
> cerr << "2 gives: --" << what[2].first << "-- and second: "
> << what[2].second << endl;
> cerr << "3 gives: --" << what[3].first << "-- and second: "
> << what[3].second << endl;

[...]

> Matched new style section redirection!!!! Input is: [MY_SECTION =
> <FILE:SECTION>]
> 0 gives: --[MY_SECTION = <FILE:SECTION>]-- and second:
> 1 gives: --MY_SECTION = <FILE:SECTION>]-- and second: =
> <FILE:SECTION>]
> 2 gives: --FILE:SECTION>]-- and second: :SECTION>]
> 3 gives: --SECTION>]-- and second: >]
>
> I don't understand why $1 is not giving my MY_SECTION only.
> It looks like the non-greedy operator is not working as I expect it.
> This same expression works in Perl and jakarta's regex engine in Java.

Your misunderstanding here is the nature of "first" and "second". These
are iterators that can be used to pinpoint where in the original string
you matched. The fact that you can stream them out to cerr without a
compiler error is a sort of "accident", and the output you are getting
is from the point where the iterator is pointing to the end of the
string. If you simply used the matches as strings, you'd see the
results you'd expect.

         cerr << "0 gives: --" << what[0] << endl;
         cerr << "1 gives: --" << what[1] << endl;
         cerr << "2 gives: --" << what[2] << endl;
         cerr << "3 gives: --" << what[3] << endl;

You only want to use what[0].first and what[0].second when you have a
reason to want to locate the part of the original string that was
matched, and even then, you won't want to use them on their own, but
rather together.

Aside to John Maddock: Perhaps these should be named something more
like begin and end rather than first and second?

     -- Darin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net