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)

  • Creator
    Topic
  • #47810
    Bob Schmid
    Participant

      I am trying to search a file with multiple ISA segments. I would like to get the offset(s) into the file at which these ISA’s exist. What wuld be the regexp that would give me hits on mulitple ISA’s at different offsets into the file. I seemd to only be able to pick one ISA up.

      Thanks ahead….

    Viewing 0 reply threads
    • Author
      Replies
      • #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

      Viewing 0 reply threads
      • The forum ‘Cloverleaf’ is closed to new topics and replies.