Perl 相当于 PHP 的 preg_callback [英] Perl equivalent of PHP's preg_callback

查看:55
本文介绍了Perl 相当于 PHP 的 preg_callback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Perl 中有一个 preg_callback 等价物吗?

Do we have a preg_callback equivalent in Perl ?

假设我想匹配某物并将其替换为使用匹配的东西调用的函数的返回值.

Lets say I want to match something and replace it with the return value of the function that is called with the matched thing.

推荐答案

使用 s///e - 评估修饰符,您可以在第二部分中放置任意的 perl 代码.

Use s///e - evaluation modifier and you can put arbitrary perl codes in second part.

$x = "this is a test";
$x =~ s/(test)/reverse($1)/eg;
print $x;

//this is a tset

ref: http://perldoc.perl.org/perlretut.html#搜索和替换

这篇关于Perl 相当于 PHP 的 preg_callback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆