regex 在任意字符之间添加逗号的正则表达式

vlju58qv  于 2022-11-18  发布在  其他
关注(0)|答案(4)|浏览(147)

我对正则表达式比较陌生,所以如果这个问题很琐碎,请耐心等待。我想使用正则表达式在字符串的每个字母之间放置一个逗号,例如:

x <- "ABCD"

我想得到

"A,B,C,D"

如果我能用gsubsub或相关的任意字符数的字符串向量来做这件事就好了。
我试过了

> sub("(\\w)", "\\1,", x)
[1] "A,BCD"
> gsub("(\\w)", "\\1,", x)
[1] "A,B,C,D,"
> gsub("(\\w)(\\w{1})$", "\\1,\\2", x)
[1] "ABC,D"
7cjasjjr

7cjasjjr1#

Try:

x <- 'ABCD'
gsub('\\B', ',', x, perl = T)

Prints:

[1] "A,B,C,D"

Might have misread the query; OP is looking to add comma's between letters only. Therefor try:

gsub('(\\p{L})(?=\\p{L})', '\\1,', x, perl = T)
  • (\p{L}) - Match any kind of letter from any language in a 1st group;
  • (?=\p{L}) - Positive lookahead to match as per above.

We can use the backreference to this capture group in the replacement.

zpqajqem

zpqajqem2#

您可以使用

> gsub("(.)(?=.)", "\\1,", x, perl=TRUE)
[1] "A,B,C,D"

(.)(?=.)正则表达式匹配将其捕获到组1中的任何字符((.)),该组1后面必须跟有任何单个字符((?=.))是一个正前瞻,它需要紧接在当前位置右侧的字符)。
溶液的变化:

> gsub("(.)(?!$)", "\\1,", x, perl=TRUE)
## Or with stringr:
## stringr::str_replace_all(x, "(.)(?!$)", "\\1,")
[1] "A,B,C,D"

这里,如果存在字符串位置的结尾,则(?!$)匹配失败。
请参阅R demo online

x <- "ABCD"
gsub("(.)(?=.)", "\\1,", x, perl=TRUE)
# => [1] "A,B,C,D"
gsub("(.)(?!$)", "\\1,", x, perl=TRUE)
# => [1] "A,B,C,D"
stringr::str_replace_all(x, "(.)(?!$)", "\\1,")
# => [1] "A,B,C,D"
vsikbqxv

vsikbqxv3#

非正则表达式友好的回答:

paste(strsplit(x, "")[[1]], collapse = ",")
#[1] "A,B,C,D"
ryevplcw

ryevplcw4#

另一种选择是使用肯定的look behind和look ahead来Assert存在前导字符和后续字符:

library(stringr)
str_replace_all(x, "(?<=.)(?=.)", ",")
[1] "A,B,C,D"

相关问题