Reply To: regexp -indices to populate subvars (search for all ISA’s)

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf regexp -indices to populate subvars (search for all ISA’s) Reply To: regexp -indices to populate subvars (search for all ISA’s)

#56769
Charlie Bursell
Participant

    You can use regexp here Bob.  You can also use the string commands.  The problem with regexp is making sure to escape any special characters.

    For example, if you know the field separator is * and the element separator is ~, you can define a pattern like:

    set pat {ISA*.*?~IEA.*?~}

    Then get all of the ISA .. IEA in a list like:

    set list [regexp -all -inline — $pat $msg]

    I sometimes prefer to use the string commands.  string commands in Tcl are highly optimized and you don’t have to worry about special characters

    First find start and end locations and put in a list of pairs

    This assumes you have extracted the field, component and element separators into variables

    set locList {}; set offset 0

    set ndx [string first “ISA$fldSep” $msg $offset”

    while {$ndx >= 0} {

         lappend locList $ndx

         incr offset 4       ;# Step past last ISA

          set ndx [string first “ISA$fldSep” $msg $offset]

          if {$ndx >= 0} {

                lappend locList [expr $ndx – 1]

          }

    }

    # Now put end as end of last one if any

    if {![lempty $locList} {lappend locList end}

    # Now you can extract your ISA .. IEA

    set msgList {}

    foreach {start end} $loclist {

        lappend msgList [string range $msg $start $end]

    }

    I hope you get the idea here.  You could combine these steps but I find it more self documenting this way.

    FWIW, according to Jeff Hobbs at Active State, there is no command you can execute that is more optimized than the string commands.

    Hope this helps

    Charlie