变量中的tcl长度

时间:2017-10-27 17:25:56

标签: tcl textutils

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

因此,仅在50个字符之后的新行中拆分

svg

此致

编辑:

输入是变量$ Plot

中的此文本
set length [::textutil::adjust $text -length 50 -strictlength true]

仅输出:

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"

不幸的是不再......

2 个答案:

答案 0 :(得分:1)

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

set text [string repeat 123456 48]
set formatted [regsub -all {.{50}} $text "&\n"]
puts $formatted
12345612345612345612345612345612345612345612345612
34561234561234561234561234561234561234561234561234
56123456123456123456123456123456123456123456123456
12345612345612345612345612345612345612345612345612
34561234561234561234561234561234561234561234561234
56123456123456123456123456123456123456

答案 1 :(得分:1)

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

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

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

如果您想限制字边界,RE最好更改为\m.{1,50}\M