是否有支持内联代码的regex_replace的变体? [英] Is There a Variant of regex_replace that Supports Inline Code?
问题描述
Perl有 e
regex修饰符,允许Perl代码,而不只是一个字符串来形成替换: http://perldoc.perl.org/perlretut.html#Search-and-replace 虽然该示例并不是最大的,开关来实现这一点。对于那些理解Perl的人来说,这里的例子更有意义:
Perl has the e
regex modifier which allows Perl code rather than just a string to formulate the replacement: http://perldoc.perl.org/perlretut.html#Search-and-replace Though that example is not the greatest as there are switches to accomplish this. For those of you who understand Perl here's an example that makes more sense:
$string = "StackOverflow user: Jonathan Mee";
$string =~ s/:\s*(.*)$/$1 == "Jonathan Mee" ? ": ".$1." is AWESOME!" : ": ".$1." is ???"/e;
print $string; #Will print "StackOverflow user: Jonathan Mee is AWESOME!"
在C ++中有一个 regex_replace
会让我做类似的事情吗?
Is there a regex_replace
variant in C++ that will allow me to do something similar? As in code inline for the replacement.
推荐答案
regex_replace
有6种不同的重载。 fmt
参数对于每个参数使用相同的方式:
regex_replace
has 6 different overloads. The fmt
argument is used the same way for each of them:
与每个匹配的替换。
这可能包括由它们代表的字符替换的格式说明符和转义序列。
A string with the replacement for each match. This may include format specifiers and escape sequences that are replaced by the characters they represent.
sport a flags
参数,用于控制,如何格式化 fmt
。 fmt
相关选项包括:
Each of the 6 overloads also sport a flags
argument which is used to control, "how fmt
is formatted." The fmt
related options are:
-
format_default
:默认格式
使用标准格式化规则替换匹配(由ECMAScript的替换方法使用的匹配)。 -
format_sed
sed格式化
使用与POSIX中的sed实用程序相同的规则替换匹配项。 -
format_no_copy
无副本
替换匹配时,不会复制目标序列中与正则表达式不匹配的部分。 -
format_first_only
首先只有
只替换第一次出现的正则表达式。
format_default
: Default formatting
Uses the standard formatting rules to replace matches (those used by ECMAScript's replace method).format_sed
sed formatting
Uses the same rules as the sed utility in POSIX to replace matches.format_no_copy
No copy
The sections in the target sequence that do not match the regular expression are not copied when replacing matches.format_first_only
First only
Only the first occurrence of a regular expression is replaced.
请注意, fmt
重载和标志
都不支持内嵌代码。因此,答案是:不,没有支持在线代码的 regex_replace
变体。
Note that none of the fmt
overloads, nor the flags
support in-line code. So the answer is: No, there is no variant of regex_replace
that supports in-line code.
但是,如果您愿意使用STD算法与 regex_iterator
,您可以使用lambda来完成在线代码。
However if you were willing to use a STD algorithm in conjunction with regex_iterator
, you could use a lambda to accomplish in-line code.
const string foo("StackOverflow user: Jonathan Mee");
vector<string> bar;
transform(regex_iterator<string::const_iterator>(foo.cbegin(), foo.cend(), regex(".*:\\s*(.*)")),
regex_iterator<string::const_iterator>(),
back_inserter(bar),
[](const smatch& i){auto result = i.str();
if (!result.empty()){
result += (i.str(1) == "Jonathan Mee" ? " is AWESOME!" : " is ???");
}
return result;});
正如你可以看到一个lambda可以在 transform
接受当前 regex_iterator
的 smatch
。这对于多行字符串非常可扩展,在 string foo(StackOverflow user:Jonathan Mee \\\
的输出将是:
StackOverflow user:user0);
As you can see a lambda is usable in the transform
taking in the current regex_iterator
's smatch
. This is very extensible to multi-line strings, in the example of string foo("StackOverflow user: Jonathan Mee\nStackOverflow user: user0");
the output would be:
StackOverflow用户:Jonathan Mee很棒!
StackOverflow用户:user0是
StackOverflow user: Jonathan Mee is AWESOME!
StackOverflow user: user0 is ???
很明显,只有一个 smatch
和 regex_replace
。其中 str(1)
属于中未指定str()
。在这里我利用了事实,它是正确的在 foo
的结尾,而不是某处必须在 foo
。但是应该提到的是,同样的困难降低了Perl的 e
-modifier,所以我认为这是非常相似的。
Clearly there are some trade-offs working with just an smatch
versus regex_replace
. Where str(1)
falls within str()
is not specified. Here I take advantage of the fact that it is right at the end of foo
rather than somewhere that has to be found in the middle of foo
. But it should be mentioned that the same difficulty befalls Perl's e
-modifier, so I think this is pretty much on par.
这篇关于是否有支持内联代码的regex_replace的变体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!