0

如何总是在 50 个字符后拆分变量中的文本?

所以仅在 50 个字符后拆分为新行

set length [::textutil::adjust $text -length 50 -strictlength true]

问候

编辑:

输入是变量 $Plot 中的文本

LaRochelle, a former pirate captain, is caught by the British. To get his 
ship back, he works as a spy against other pirates, first of all Blackbeard 
and Providence. He works on some ships, crossing the Caribbean sea, with the 
intention


set pieces [regexp -all -inline {.{1,50}} $Plot]
set 0 [lindex [lindex $pieces 0] 0]
set 1 [lindex [lindex $pieces 1] 1]
putnow "PRIVMSG $channel :$0"

输出只有:

<testbot> LaRochelle,
<testbot> British.

可惜没有了。。

4

2 回答 2

1

您可以使用regsub在每 50 个字符后添加一个换行符。

set text [string repeat 123456 48]
set formatted [regsub -all {.{50}} $text "&\n"]
puts $formatted
12345612345612345612345612345612345612345612345612
34561234561234561234561234561234561234561234561234
56123456123456123456123456123456123456123456123456
12345612345612345612345612345612345612345612345612
34561234561234561234561234561234561234561234561234
56123456123456123456123456123456123456
于 2017-10-27T20:26:56.777 回答
1

最简单的方法是regexp -all -inline用于拆分,因为它具有所有匹配(以及子匹配,如果存在)的列表的结果,这意味着它可以非常直接地提供所需的结果:

set pieces [regexp -all -inline {.{1,50}} $inputString]

RE 是.{1,50}(在大括号中;在 Tcl 中技术上不必要,但几乎总是一个好主意),这意味着“尽可能多的一到五十个字符(因为贪婪匹配)”,我们尽可能多地得到这些字符。


如果要限制单词边界,最好将 RE 更改为\m.{1,50}\M.

于 2017-10-28T11:04:28.490 回答