Is there a regex for preserving case pattern in the vein of U
and L
?
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"
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)
)
)
The goal is to have a single pattern
and a single replace
such as
gsub("(date)", "\Umonth", input, perl=TRUE)
which will yield the desired output
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…