它可能的c ++正则表达式评估器与lambdas像ruby? [重复]

时间:2022-11-26 20:42:17

This question already has an answer here:

这个问题在这里已有答案:

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

我试图学习如何使用lambda表达式在c ++中编写正则表达式求值器

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

如果可以使用lambda表达式,我该如何做到这一点

Please help me

请帮我

1 个解决方案

#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.

在所有正则表达式中,只需坐在regex_search循环中并根据每个匹配在其中构建新输出,就可以很容易地模拟它。

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

好吧,就C ++(> = 11)而言,_你不能提供回调来自动执行此操作!很伤心吧..

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

(Boost :: Regex将其内置到正则表达式替换函数as和选项(回调函子)中。)

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.

你必须滚动你自己的一般regex_replace()类,它接受一个回调函数,它不会像他们所描述的那样做什么。

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

幸运的是,有人已经使用C ++的所有铃声和口哨完成了这项工作。

regex replace with callback in c++11?

正则表达式替换为c ++ 11中的回调?

Enjoy !!

#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.

在所有正则表达式中,只需坐在regex_search循环中并根据每个匹配在其中构建新输出,就可以很容易地模拟它。

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

好吧,就C ++(> = 11)而言,_你不能提供回调来自动执行此操作!很伤心吧..

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

(Boost :: Regex将其内置到正则表达式替换函数as和选项(回调函子)中。)

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.

你必须滚动你自己的一般regex_replace()类,它接受一个回调函数,它不会像他们所描述的那样做什么。

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

幸运的是,有人已经使用C ++的所有铃声和口哨完成了这项工作。

regex replace with callback in c++11?

正则表达式替换为c ++ 11中的回调?

Enjoy !!