RegExp - Callback function

Hello.
First... I'm sorry for my english. And second - I've got small problem.
I need use regexp. The idea is that I use 'replace', it find some text like in expression and this will be an input for function... then in function something happen and output of the function replace in text the input of the function...
For example I've got text "Solution: 5 and 4". Regular expression is "[0-9]" And the function is "add5"
int add5(int input){return input+5}
And the output of this function replace the number with number+5... And in the end text will be: "Solution: 10 and 9"
In C++ with Boost? I don't know how can I do it...
something like this....
#include <cstdlib>
#include <iostream>
#include

Golan Trevize wrote:
// the callback function string add5(string x) { int v; v =atoi(x.c_str()) + 5; string vystup = (char *)v; return vystup; }
I'm not sure about the regex problem, but I think that the callback doesn't do what you think it does. The first part does convert the number in x into its value + 5 in v, but the the second part doesn't convert v into a string. I think Boost.Lexical cast will help here, or you can use a std::stringstream: std::stringstream ss; ss << v; string vystup = ss.str(); You used a C style cast, (char *)v, which is generally a pretty bad idea in C++. The C++ casts (static_cast, dynamic_cast etc.) have better type safety than the C ones. By using a C cast you've turned off the type checking, but the type checker was trying to tell you something about the expression - shutting it up doesn't make the problem go away. K

Golan Trevize wrote:
I need use regexp. The idea is that I use 'replace', it find some text like in expression and this will be an input for function... then in function something happen and output of the function replace in text the input of the function...
For example I've got text "Solution: 5 and 4". Regular expression is "[0-9]" And the function is "add5" int add5(int input){return input+5} And the output of this function replace the number with number+5... And in the end text will be: "Solution: 10 and 9"
In C++ with Boost? I don't know how can I do it...
You need to iterate over the matches and replace each occurance with your new text, off the top of my head something like: extern std::string text_to_search; std::string result_text; boost::regex r("[[:digit:]]+"); boost::sregex_iterator a(text_to_search.begin(), text_to_search.end(), r), b; std::string::const_iterator last_end = text_to_search.begin(); while(a != b) { result_text.append(a->prefix()); result_text.append(my_transformation(a.str())); last_end = (*a)[0].second; ++a; } result_text.append(last_end, text_to_search.end()); The key part here is my_transformation(x) which should accept a string and return the new string: use lexical_cast to convert between strings and numbers as required. HTH, John.

Hello. Thanks you very much. It was only stupid example, I need it for something complete diferent. And I couldn't find the right solution. I know, how do this in PHP, where it's easy... In C++ it was for me problem... Thanks once more.
------------ Původní zpráva ------------ Od: John Maddock
Předmět: Re: [Boost-users] RegExp - Callback function Datum: 27.10.2007 17:37:59 ---------------------------------------- Golan Trevize wrote: I need use regexp. The idea is that I use 'replace', it find some text like in expression and this will be an input for function... then in function something happen and output of the function replace in text the input of the function...
For example I've got text "Solution: 5 and 4". Regular expression is "[0-9]" And the function is "add5" int add5(int input){return input+5} And the output of this function replace the number with number+5... And in the end text will be: "Solution: 10 and 9"
In C++ with Boost? I don't know how can I do it...
You need to iterate over the matches and replace each occurance with your new text, off the top of my head something like:
extern std::string text_to_search; std::string result_text; boost::regex r("[[:digit:]]+");
boost::sregex_iterator a(text_to_search.begin(), text_to_search.end(), r), b; std::string::const_iterator last_end = text_to_search.begin();
while(a != b) { result_text.append(a->prefix()); result_text.append(my_transformation(a.str())); last_end = (*a)[0].second; ++a; } result_text.append(last_end, text_to_search.end());
The key part here is my_transformation(x) which should accept a string and return the new string: use lexical_cast to convert between strings and numbers as required.
HTH, John.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Golan Trevize
-
John Maddock
-
Kirit Sælensminde