如何使用 PCRE2 实现 /e 修饰符?
How to implement /e modifier with PCRE2?
在 Perl 中,我们可以做到这一点
s/pattern/func()/e
有没有类似PCRE2的便捷函数,比如
::pcre2_substitute_with_callback(
re, // the compiled pattern
pcuSubject, ccuSubject, // the subject and its length
PCRE2_SUBSTITUTE_GLOBAL, // the substitute options
matches,
NULL, // the match context
[](PCRE2_SPTR pcuMatched)->PCRE2_SPTR{ // the callback
return "replacement";
},
pcuResult, &ccuResult
);
谢谢。
不,我认为pcre2
中没有这样的便利。不过请参阅下面的包装纸。
但是,我相信可以在没有任何特别限制的情况下准备调用 pcre2_substitute 的替换字符串。 (我现在无法测试。)明确指定了使用转义字符 ($
) 来捕获组或模式项,但我不明白为什么不能在 function/callback 中使用它来形成替换字符串。
然后可以将其包装在具有所需签名的方法中。
来自 pcre2api
的更多文档位于 Creating a new string with substitutions
有一个用于此目的的 C++ 包装器 JPCRE2. It uses the replace
method of RegexReplace。但是,关于 half-way 通过主页它还通知我们
There's another replace function (jp::RegexReplace::nreplace()
) that takes a MatchEvaluator with a callback function. It's required when you have to create the replacement strings dynamically according to some criteria.
The class jp::MatchEvaluator
implements several constructor overloads to take different callback functions.
该页面继续使用 jp::RegexReplace::nreplace() 的完整示例。
发行版中的测试文件中提供了更详细的示例。
在 Perl 中,我们可以做到这一点
s/pattern/func()/e
有没有类似PCRE2的便捷函数,比如
::pcre2_substitute_with_callback(
re, // the compiled pattern
pcuSubject, ccuSubject, // the subject and its length
PCRE2_SUBSTITUTE_GLOBAL, // the substitute options
matches,
NULL, // the match context
[](PCRE2_SPTR pcuMatched)->PCRE2_SPTR{ // the callback
return "replacement";
},
pcuResult, &ccuResult
);
谢谢。
不,我认为pcre2
中没有这样的便利。不过请参阅下面的包装纸。
但是,我相信可以在没有任何特别限制的情况下准备调用 pcre2_substitute 的替换字符串。 (我现在无法测试。)明确指定了使用转义字符 ($
) 来捕获组或模式项,但我不明白为什么不能在 function/callback 中使用它来形成替换字符串。
然后可以将其包装在具有所需签名的方法中。
来自 pcre2api
的更多文档位于 Creating a new string with substitutions
有一个用于此目的的 C++ 包装器 JPCRE2. It uses the replace
method of RegexReplace。但是,关于 half-way 通过主页它还通知我们
There's another replace function (
jp::RegexReplace::nreplace()
) that takes a MatchEvaluator with a callback function. It's required when you have to create the replacement strings dynamically according to some criteria.The class
jp::MatchEvaluator
implements several constructor overloads to take different callback functions.
该页面继续使用 jp::RegexReplace::nreplace() 的完整示例。
发行版中的测试文件中提供了更详细的示例。