Boost logo

Boost Users :

Subject: [Boost-users] Regex Design trap
From: Sven Bauhan (Sven.Bauhan_at_[hidden])
Date: 2010-09-08 04:44:28


Dear Developers,

I found a possible trap in the design of the syntax of the Regex library.

Consider the following code:
std::string text( "blabla123xyz" );
boost::regex expression( "\\w+(\\d+)\\w+" );
boost::smatch matches;
boost::regex_search( text, matches, expression );
text = "asdfghjkl";
std::string value = matches[1];

Although this code is not very useful, it can lead to inpredictable behaviour.
As far as i know the matches just reference the string position in the
original string. so when the string is changed the matches don't fit any
more. This may be a quite good performance but it requires to be very
careful. Especially if the string is just referenced somewhere and the
matches are given to somewhere else.

Furthermore as i saw the Regex library I wondered about its interface. It
seems more like a C library interface than C++ code. I also code in Ruby and
the Regex class is much more convenient. The pattern matching is done there
by a method of class Regex and returns the matches:
expression = Regex.new( "\w+(\d+)\w" )
matches = expression.match( "blabla123xyz" )
if ( matches ) ...

Would it be possible to implement such a more object oriented interface to
boost::regex?

Greetings, Sven

-- 
Dipl.-Ing. Sven Bauhan
DFS Deutsche Flugsicherung GmbH

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