用于保留大小写模式、大写的正则表达式 [英] regex for preserving case pattern, capitalization
问题描述
是否有一个正则表达式可以在 \U
和 \L
的脉络中保留大小写模式?
Is there a regex for preserving case pattern in the vein of \U
and \L
?
在下面的示例中,我想将 "date"
转换为 "month"
,同时保持 input
中使用的大写>
In the example below, I want to convert "date"
to "month"
while maintaining the capitalization used in the input
from to
"date" ~~> "month"
"Date" ~~> "Month"
"DATE" ~~> "MONTH"
我目前使用三个对 sub
的嵌套调用来完成此操作.
I currently use three nested calls to sub
to accomplish this.
input <- c("date", "Date", "DATE")
expected.out <- c("month", "Month", "MONTH")
sub("date", "month",
sub("Date", "Month",
sub("DATE", "MONTH", input)
)
)
目标是有一个pattern
和一个replace
,比如
The goal is to have a single pattern
and a single replace
such as
gsub("(date)", "\\Umonth", input, perl=TRUE)
这将产生所需的输出
推荐答案
这是我认为 for
循环是合理的情况之一:
This is one of those occasions when I think a for
loop is justified:
input <- rep("Here are a date, a Date, and a DATE",2)
pat <- c("date", "Date", "DATE")
ret <- c("month", "Month", "MONTH")
for(i in seq_along(pat)) { input <- gsub(pat[i],ret[i],input) }
input
#[1] "Here are a month, a Month, and a MONTH"
#[2] "Here are a month, a Month, and a MONTH"
还有一个由 @flodel
提供的替代方案,通过 Reduce
实现与循环相同的逻辑:
And an alternative courtesy of @flodel
implementing the same logic as the loop through Reduce
:
Reduce(function(str, args) gsub(args[1], args[2], str),
Map(c, pat, ret), init = input)
有关这些选项的一些基准测试,请参阅@TylerRinker 的回答.
For some benchmarking of these options, see @TylerRinker's answer.
这篇关于用于保留大小写模式、大写的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!