View Full Version : [C/CPP] Can this be done? Replacing words..

07-08-2009, 07:25 AM
I have an ASCII file input.sql which contains a database dump. I want that all the 5 lettered words that fall between a left parentheses i.e. ( and a comma i.e. , be replaced by the string (NULL,

I did that with ease in PHP, but in c/cpp, it seems difficult.

Here's the PHP code:

$blah1=preg_replace('/\(.....,/', '(NULL,', $blah);
$blah2=fopen("output.sql", "a+");
fwrite($blah2, $blah1);

Any ideas people? I cant figure it out..


ralph l mayo
07-13-2009, 04:08 PM
It's easily done in C++ with boost::regex_replace (http://www.boost.org/doc/libs/1_35_0/libs/regex/doc/html/boost_regex/ref/regex_replace.html)

It's also rather easily done in either language using a state machine. Just loop the characters and start counting at each left paren. When the next comma comes up (or the count gets too high) replace if the count indicates the pattern has been matched, otherwise reset and carry on looking for the next paren.