我试图在每个大写字母之前添加下划线,然后是小写。这是一个例子:
cases <- c("XrefAcctnoAcctID", "NewXref1AcctID", "NewXref2AcctID", "ClientNo")
我有这个:
[1] "XrefAcctnoAcctID" "NewXref1AcctID"
[3] "NewXref2AcctID" "ClientNo"
我想要这个:
"xref_acctno_acct_id"
"new_xref1_acct_id"
"new_xref2_acct_id"
"client_no"
我能走到这一步:
> tolower(gsub("([a-z])([A-Z])", "\\1_\\2", cases))
[1] "xref_acctno_acct_id" "new_xref1acct_id"
[3] "new_xref2acct_id" "client_no"
但"new_xref1acct_id"
"new_xref2acct_id"
并未反映出我想要的内容。
答案 0 :(得分:7)
我们可以使用正则表达式外观来匹配显示小写字母或数字后跟大写字母的模式,并将其替换为_
tolower(gsub("(?<=[a-z0-9])(?=[A-Z])", "_", cases, perl = TRUE))
#[1] "xref_acctno_acct_id" "new_xref1_acct_id" "new_xref2_acct_id"
#[4] "client_no"
或者没有外观,我们可以将小写或数字作为一组捕获,然后将大写字母作为另一组捕获,并将其替换为由_
tolower(gsub("([a-z1-9])([A-Z])", "\\1_\\2", cases))
#[1] "xref_acctno_acct_id" "new_xref1_acct_id" "new_xref2_acct_id"
#[4] "client_no"