如何用连字符替换大写后的小写字母

时间:2014-05-04 17:10:03

标签: regex r substitution uppercase

在我的数据框中,我有一个列,其中parlement成员的姓氏是小写的。我用(来自this answer

替换了第一个带有大写字母的大写字母
# vector with names
lastname <- c("wortmann-kool", "mulder", "nistelrooij", "camp", "schaake", "veld", "lange", "oomen-ruijten")
# substituting first letter with uppercase
lastname <- gsub("^(\\w)(\\w+)", "\\U\\1\\L\\2", lastname, perl = TRUE)

正如你所看到的,有些名字用连字符分隔已婚女性的两个名字。如何用连字符替换大写后的第一个字母?

2 个答案:

答案 0 :(得分:4)

为什么不简单地将word boundary之后的第一个字母大写?

> lastname <- c("wortmann-kool", "mulder", "nistelrooij", "camp", "schaake", "veld", "lange", "oomen-ruijten")
> gsub("\\b(\\w)", "\\U\\1", lastname, perl = TRUE)
[1] "Wortmann-Kool" "Mulder"        "Nistelrooij"   "Camp"         
[5] "Schaake"       "Veld"          "Lange"         "Oomen-Ruijten"
> 

引用documentation

  

仅对于perl = TRUE,它还可以包含“\ U”或“\ L”来转换   其余的替换为大写或小写,“\ E”结束   转换。

## capitalizing
txt <- "a test of capitalizing"
gsub("(\\w)(\\w*)", "\\U\\1\\L\\2", txt, perl=TRUE)
gsub("\\b(\\w)",    "\\U\\1",       txt, perl=TRUE)

答案 1 :(得分:1)

这适用于您可能存在的任何标点字符

gsub("(^|[[:punct:]])([[:alpha:]])", "\\1\\U\\2", lastname, perl=TRUE)

##[1] "Wortmann-Kool" "Mulder"        "Nistelrooij"   "Camp"         
##[5] "Schaake"       "Veld"          "Lange"         "Oomen-Ruijten"

这仅适用于连字符

gsub("(^|-)([[:alpha:]])", "\\1\\U\\2", lastname, perl=TRUE)

##[1] "Wortmann-Kool" "Mulder"        "Nistelrooij"   "Camp"         
##[5] "Schaake"       "Veld"          "Lange"         "Oomen-Ruijten"