【发布时间】:2022-02-11 07:31:24
【问题描述】:
在 SO 我找到了一个解决方案,它有助于在字符串中的每个第 n 个字符插入一个值/字符:
(?=(?:.{n})+$)
但是每隔n个空格插入一个值(例如,制表符或\n)会更合理,这样单词就不会被拆分。编辑此正则表达式的可能方法是什么?
我进行了聚类分析,现在我想将标签附加到树状图中。考虑标签是很长的字符串,例如:
tibble(
id = d2022_1,
label = "A very long label for the dendro that should be splitted so it will look nicely in the picture"
)
我想让它按行列表/拆分,所以我想插入\n:
A very long label for the dendro\nthat should be splitted so\nit will look nicely in the picture
【问题讨论】:
-
类似
cat(gsub("(\\S+(?:\\s+\\S+){2})\\s*", "\\1\n", x))的东西分成三个字(n-1={2})?