Pages

Regular expression with boost

As an introduction to some of the most interesting boost libraries you can read "Beyond the C++ Standard Library: An Introduction to Boost", by Björn Karlsson, an Addison Wesley Professional book. That's what I'm actually doing, and these are a few notes that I'm jotting down in the meanwhile.

The boost regex library is one of the few ones in that family that requires to be compiled and linked to our application to be used.

If you are using a popular compiler, boost provides makefiles and appropriate information to do that, have a look at the documentation for more details. In my current case, I'm using gcc and Code::Blocks as IDE. Once I generated the libs I set the project in the IDE in this way:

Through menu Project - Properties - button "Project's build options" - tab "Search directories" (for all the project configuration) - subtab "Compiler" add the base boost directory for the include files (in my case: "C:\dev\boost_1_40_0").

On the "Linker" subtab add the directory where we have generated the regex libraries (for me: "C:\dev\boost_1_40_0\libs\regex\build\gcc").

On the "Liker setting" tab we should now add the libraries, pay attention to insert debug and release version for the relative configuration.

Done that, we should just include the header file "boost/regex.hpp" to have access to the regular expression functionality.

A regular expression is created in our source code in this way:

boost::regex reg("(A.*)");

Now we have a (referenciabile) regular expression that is matching against any string starting with a capital A.

No comments:

Post a Comment