boost::regex_search matching extra characters?

Discussion in 'C++' started by A, Aug 27, 2010.

  1. A

    A Guest

    I probably missed something stupid but why is this matching extra
    characters?

    if (boost::regex_search("bla bla (message #12345)",
    matches,
    boost::regex("\\(message #([0-9]+)\\)$")))
    {
    //matches[1].first contains "12345)" but regex is supposed to match only
    "12345"
    //matches[1].second contains ")"
    }

    I've tested regex in php so the regex should not match those extra chars
    AFAIK

    I solved this by removing the length of matches[1].second from first string
    but is there something wrong with the above code or this is simply how
    boost::regex works?
     
    A, Aug 27, 2010
    #1
    1. Advertising

  2. Am 27.08.2010 22:21, schrieb A:
    > I probably missed something stupid but why is this matching extra
    > characters?
    >
    > if (boost::regex_search("bla bla (message #12345)",
    > matches,
    > boost::regex("\\(message #([0-9]+)\\)$")))
    > {
    > //matches[1].first contains "12345)" but regex is supposed to match only
    > "12345"
    > //matches[1].second contains ")"
    > }


    Do you know what first and second are supposed to represent here?

    first is the start of the matching sequence, second is one-past the
    matching sequence.
    This means that first and second are the same as begin() and end() of
    all the C++ containers.

    To extract the matching string, you can do:

    std::string match(matches[1].first, matches[1].second);

    --
    Thomas
     
    Thomas J. Gritzan, Aug 27, 2010
    #2
    1. Advertising

  3. A

    A Guest

    > first is the start of the matching sequence, second is one-past the
    > matching sequence.
    > This means that first and second are the same as begin() and end() of
    > all the C++ containers.


    well, i'm also into php where this is interpreted a little bit different and
    where matches are usually what they are supposed to be - matches, so mixing
    2 languages can get sometimes a bit confusing.

    thank you for the explanation.
     
    A, Aug 28, 2010
    #3
  4. A <>, on 28/08/2010 05:21:49, wrote:

    >> first is the start of the matching sequence, second is one-past the
    >> matching sequence.
    >> This means that first and second are the same as begin() and end() of
    >> all the C++ containers.

    >
    > well, i'm also into php where this is interpreted a little bit different and
    > where matches are usually what they are supposed to be - matches


    Well, why, are they anything different?

    The difference resides in how they are reported, and this information
    usually is where it is supposed to be - in the documentation ;-)

    http://www.boost.org/doc/libs/1_44_0/libs/regex/doc/html/boost_regex/ref/regex_search.html

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Aug 28, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Latter
    Replies:
    2
    Views:
    2,912
    Julie
    May 17, 2004
  2. Stou Sandalski
    Replies:
    0
    Views:
    366
    Stou Sandalski
    Apr 19, 2007
  3. ¿­ÖÐ
    Replies:
    4
    Views:
    499
    James Kanze
    May 11, 2008
  4. mathieu
    Replies:
    3
    Views:
    602
    Bo Persson
    Sep 4, 2009
  5. G
    Replies:
    7
    Views:
    143
    Ben Morrow
    Feb 12, 2004
Loading...

Share This Page