每> n个字符替换子字符串(有条件地插入换行符以空格) [英] Replace substring every >n characters (conditionally insert linebreaks for spaces)

查看:99
本文介绍了每> n个字符替换子字符串(有条件地插入换行符以空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中相当长的chracter向量中用换行符(\n)替换空格.但是,我不想替换每个空间,但前提是子字符串必须存在一定数量的字符(n).

I would like to replace spaces with linebreaks (\n) in a pretty long chracter vector in R. However, I don't want to replace every space, but only if the substring exeeds a certain number of characters (n).

示例:

mystring <- "this string is annoyingly long and therefore I would like to insert linebreaks" 

现在,我希望每个子字符串的长度都大于20个字符(nchar > 20)的情况下,在每个空格的mystring中插入换行符.

Now I want to insert linebreaks in mystring at every space on the condition that each substring has a length greater than 20 characters (nchar > 20).

因此,生成的字符串应该看起来像这样:

Hence, the resulting string is supposed to look like this:

"this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks") 

在25、26和25个字符后插入了换行符(\n).

Linebreaks (\n) were inserted after 25, 26 and 25 characters.

我该如何实现? 也许是结合了gsubstrsplit的东西?

How can I achieve this? Maybe something combining gsub and strsplit?

推荐答案

您可以使用.{21,}?\s正则表达式匹配任意21个(或以下)字符(由于nchar > 20起),但应尽可能少,直到最近的空格:

You may use .{21,}?\s regex to match any 21 (since nchar > 20) chars or more, but as few as possible, up to the nearest whitespace:

> gsub("(.{21,}?)\\s", "\\1\n", mystring)
[1] "this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks"

详细信息:

  • (.{21,}?)-第1组,捕获任何21个或更多字符,但应尽可能少(因为{21,}? lazy 量词)
  • \\s-空格
  • (.{21,}?) - Group 1 capturing any 21 chars or more, but as few as possible (as {21,}? is a lazy quantifier)
  • \\s - a whitespace

替换包含对组1的向后引用以在空白之前重新插入文本,以及换行符char(如果需要,也可以随意添加CR).

The replacement contains the backreference to Group 1 to reinsert the text before the whitespace, and the newline char (feel free to add CR, too, if needed).

这篇关于每> n个字符替换子字符串(有条件地插入换行符以空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆