Any Boost Experts out there for Boost.Regex?

R

Richard Latter

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
 
J

John Harrison

Richard Latter said:
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
 
J

Julie

Richard Latter wrote:
[snip boost-related stuff]

Your question would be more topical and you would probably get better answers
on the Boost newsgroup:

news://news.gmane.org/gmane.comp.lib.boost.devel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top