简体   繁体   中英

Tcl: how to split string by blank lines

could you please help me split a string into a list using as a delimiter a blank line ? split using "\\n\\n" doesn't seem to work. an example of such a string is the following :

Group: 1.1.1.1, (?)
   Source: 2.2.2.2 (?)
     Rate: 5382 pps/58470 kbps(1sec), 58469 kbps(last 30 secs), 58327 kbps(life avg)

Group: 3.3.3.3, (?)
   Source: 4.4.4.4 (?)
     Rate: 9150 pps/99399 kbps(1sec), 99398 kbps(last 30 secs), 85769 kbps(life avg)

Group: 5.5.5.5, (?)
   Source: 6.6.6.6 (?)
     Rate: 474 pps/5163 kbps(1sec), 5164 kbps(last 30 secs), 5144 kbps(life avg)

You may use the regular expression ^\\s*$ to match all lines that only contain zero to N white space. Normally ^ and $ match start and end of line, unless MULTILINE mode is active.

set mystring {Group: 1.1.1.1, (?)
    Source: 2.2.2.2 (?)
      Rate: 5382 pps/58470 kbps(1sec), 58469 kbps(last 30 secs), 58327 kbps(life avg)

Group: 3.3.3.3, (?)
    Source: 4.4.4.4 (?)
      Rate: 9150 pps/99399 kbps(1sec), 99398 kbps(last 30 secs), 85769 kbps(life avg)

Group: 5.5.5.5, (?)
    Source: 6.6.6.6 (?)
      Rate: 474 pps/5163 kbps(1sec), 5164 kbps(last 30 secs), 5144 kbps(life avg)}

set mylist {}
while {[regexp "(.*)\n\n(.*)" $mystring match part1 part2]} {
    lappend mylist $part2
    set mystring $part1
}
lappend mylist $part1

mylist now holds each 3 groups of non-blank lines in reverse order . There probably is some un-greedy regexp that will get it in order but I can't figure it out right now. One may always invert the list later, or replace the lappend lines by set , should the order be important.

Using the textutil package from Tcllib , you can do this:

package require textutil
set paragraphs [textutil::splitx $data {(?n)^\s*\n}]

The textutil::splitx splits a string up by a regular expression, and here we supply a regular expression which matches a sequence of one-or-more visually-blank lines. (The (?n) means that ^ matches line-start instead of its usual string-start meaning; that's exactly what we want here.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM