是否可以在以下向量上使用正则表达式:
u<-c("first1","sec2","thir33","fourth4","fifth25","sixth16",
"seven7","eight8","nine9","ten10","eleven11")
获得:
[1] "first.1" "sec.2" "thir3.3" "fourth.4" "fifth2.5" "sixth1.6" "seven.7" "eight.8"
[9] "nine.9" "ten.10" "eleven.11"
这与我得到的一样接近:
gsub("([A-Za-z]*)([1]{0,1})([0-9]$)","\\1\\.\\2\\3",u)
#[1] "first.1" "sec.2" "thir3.3" "fourth.4" "fifth2.5" "sixth.16" "seven.7" "eight.8" "nine.9" "ten.10"
#[11] "eleven.11"
注意第六个元素不正确:“sixth.16”应该是“sixth1.6”。