Any Boost Experts out there for Boost.Regex?

Discussion in 'C++' started by Richard Latter, May 17, 2004.

  1. Hello All,

    I am a newbie to the Boost library and I have a question about a simple
    function. All I would like to do is to create a simple function that can
    test strings using regular expressions. I've written this function which
    appears to work on the first pass and then fails on the second with the
    following message.

    Unhandle exeption in Credit Card Example.exe (KERNEL32.DLL) 0x(Some
    address): Microsoft C++ Exception.

    My function is as follows....

    bool TestStr (std::string szExpr, std::string szSearch)
    {
    bool bResult=false;

    boost::regex *cExpr = new boost::regex(szExpr);

    bResult = boost::regex_match(szSearch, *cExpr);

    delete (cExpr);

    return bResult;
    }

    Questions!

    1. Why is this failing all the time?

    2. Why in the example given in the RTL is a constant? This implies that
    the the regex object cannot be changed at run-time, which is the opposite to
    what I would like to do.

    3. Are there any examples or tutorials outside of the main library tutorial
    I could look at? Are there any newsgroups on this?

    4. Can anyone give an example of a simple POSIX calls for string matching?

    Please forgive me as I'm a newbie to all of this. Any help would be
    gratefully appreciated.

    Regards,

    Richard
     
    Richard Latter, May 17, 2004
    #1
    1. Advertising

  2. "Richard Latter" <> wrote in message
    news:c89oqb$16i$1$...
    > Hello All,
    >
    > I am a newbie to the Boost library and I have a question about a simple
    > function. All I would like to do is to create a simple function that can
    > test strings using regular expressions. I've written this function which
    > appears to work on the first pass and then fails on the second with the
    > following message.
    >
    > Unhandle exeption in Credit Card Example.exe (KERNEL32.DLL) 0x(Some
    > address): Microsoft C++ Exception.
    >
    > My function is as follows....
    >
    > bool TestStr (std::string szExpr, std::string szSearch)
    > {
    > bool bResult=false;
    >
    > boost::regex *cExpr = new boost::regex(szExpr);
    >
    > bResult = boost::regex_match(szSearch, *cExpr);
    >
    > delete (cExpr);
    >
    > return bResult;
    > }


    Why are you allocating memory dynamically?

    bool TestStr (std::string szExpr, std::string szSearch)
    {
    return boost::regex_match(szSearch, boost::regex(szExpr));
    }

    The above is simpler and likely to be more efficient. Also you should use
    const references for your strings

    bool TestStr (const std::string& szExpr, const std::string& szSearch)
    {
    return boost::regex_match(szSearch, boost::regex(szExpr));
    }

    Again this is a likely efficiency gain at no cost.

    >
    > Questions!
    >
    > 1. Why is this failing all the time?
    >


    Who knows? It could be a bug in your code, it could be a bug in
    boost::regex, it could be a bug in your compiler, it could be a bug in your
    implementation of the standard library. One thing is sure just because it
    fails on the second pass though boost does not mean that the bug is in the
    boost library. The best thing is to post a complete compilable program that
    shows the crash. Then people will be able to try it with their compilers and
    at least eliminate some of the possibilities.


    > 2. Why in the example given in the RTL is a constant? This implies that
    > the the regex object cannot be changed at run-time, which is the opposite

    to
    > what I would like to do.


    I don't think it implies that at all. It imples that in that piece of code
    the author did not want to change the regex object.

    john
     
    John Harrison, May 17, 2004
    #2
    1. Advertising

  3. Richard Latter

    Julie Guest

    Julie, May 17, 2004
    #3
    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. Ira Lee
    Replies:
    0
    Views:
    753
    Ira Lee
    Jun 29, 2004
  2. Replies:
    3
    Views:
    1,151
    techmania
    Sep 26, 2007
  3. dss
    Replies:
    2
    Views:
    408
  4. Paul

    any gridview experts out there!

    Paul, Jun 12, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    333
  5. Jim Freeze
    Replies:
    9
    Views:
    162
    YANAGAWA Kazuhisa
    Oct 21, 2003
Loading...

Share This Page