String Range forward and backward lookaround -
i trying write script gets input user , returns input in formatted area. have been using string range
function cuts the input @ range give. there way around @ specified range find next space character , cut input @ location?
for example, if have input of:
lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
my current string range
function formats input \r\n
such:
lorem ipsum dolor sit amet, consectetur dipisicing elit, sed eiusmod tempor in cididunt ut labore et dolore magna aliqua . ut enim ad minim veniam, quis nostrud e xercitation ullamco laboris
as can see on line 1 adipisicing
line 2 incididunt
words have been cut off. looking way closest space location. line 1 have been before a
on line 2 have been before i
. …in cases may after word.
is clear looking for? assistance great!
the string range
operation pretty stupid; doesn't know string splitting other contains characters. smarter splitting, best bet intelligently chosen regular expression:
set s "lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod\ tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis\ nostrud exercitation ullamco laboris." # 40 characters, word-start, word-start or end-of-string set re {\m.{1,40}(?:\m|\z)} # extract split-up list of "lines" , print them lines puts [join [regexp -all -inline $re $s] "\n"]
this produces output me:
lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
implementing full justification inserting spaces left exercise reader (because it's quite lot harder greedy line splitting!)
Comments
Post a Comment