0

Im trying to learn how I could write a regex evaluator in c++ with lambda expression

5;4
11;2
7;3
inputx.gsub(/(.*?);(.*?)\n/) {   ($1.to_i - $2.to_i ).to_s + "\n"   } 
1
9
4

How I could do this if is possible using lambda expression

Please help me

1
  • Here is another marked as Duplicate. Even though I found this first, I constructed a preamble then linked to the code. The preamble contains important information needed to understand the code in that link, as there is absolutely no explanation on how or why to use it. Unfortunately, nobody will get that benefit now, they'll just be redirected. I would have marked it as a dup otherwise. You just failed the purpose of a help center !! Commented Jun 18, 2017 at 23:03

1 Answer 1

0

The little dirty secret is that all regex replace functions of all
languages maintain an internal string by which the output is constructed
from scratch.

The output is a catenation of between-match substrings plus the matched
string in formatted form.

The new string is then returned to the caller.

But, what if you want to supply a callback function to do your own
formatting that requires language constructs ?

In all of regex land, it's easy to simulate this by just sitting in a
regex_search loop and constructing your new output inside there,
based on each match.

Well, as far as C++(>=11) is concerned, _you can't provide a callback to
do this automatically !!
Pretty sad huh..

(Boost::Regex has this built into their regex replace function as
and option (callback functor).)

So, what do you do?

You have to roll your own general regex_replace() class that takes a
callback function, which does nothing more that what they all do as described.

Lucky for you someone has already done this using all the bells and whistles of C++.

regex replace with callback in c++11?

Enjoy !!

Sign up to request clarification or add additional context in comments.

1 Comment

very thanks for all

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.