用新行替换其他 space

replace every other space with new line

我有这样的字符串:

a <- "this string has an even number of words"
b <- "this string doesn't have an even number of words"

我想用一个新行替换所有其他 space。所以输出看起来像这样...

myfunc(a)
# "this string\nhas an\neven number\nof words"
myfunc(b)
# "this string\ndoesn't have\nan even\nnumber of\nwords"

我通过 strsplitpaste 在偶数单词上换行,然后 paste(a, collapse=" ") 将它们重新组合成一个字符串来完成此操作。是否有与 gsub 一起使用的正则表达式可以完成此操作?

@Jota 推荐了一个简洁明了的方法:

myfunc  = function(x) gsub("( \S+) ", "\1\n", x)       # Jota's    
myfunc2 = function(x) gsub("([^ ]+ [^ ]+) ", "\1\n", x) # my idea

lapply(list(a,b), myfunc)


[[1]]
[1] "this string\nhas an\neven number\nof words"

[[2]]
[1] "this string\ndoesn't have\nan even\nnumber of\nwords"

它是如何工作的。 "([^ ]+ [^ ]+) " 正则表达式的想法是 (1) "find two sequences of words/nonspaces with a space between them and a space after them" 和 (2) "replace the trailing space with a newline".

@Jota 的 "( \S+) " 比较棘手——它会找到前后带有 space 的任何单词,然后用换行符替换尾随的 space。这是有效的,因为它捕获的第一个词是字符串的第二个词;并且它捕获的下一个单词不是第三个(因为我们在处理第二个单词时已经"consumed"/查看了第三个单词前面的space),而是第四个;等等。

哦,还有一些基本的正则表达式。

  • [^xyz] 表示除字符 x、y 和 z 之外的任何单个字符。
  • \s 是 space,而 \S 不是 space
  • x+表示x一次或多次
  • (x)"captures"x,允许替换时参考,如\1