grab values between special characters in a string

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf grab values between special characters in a string

  • Creator
    Topic
  • #54518
    Dustin Sayes
    Participant

      Hello,

      I have a string. The string has a common format, and I need to grab the value out of a specific place in the sting. The values will always be between the ^ and the _.

      Example string:

      NA^MICRO_HUN

      I want to read “MICRO” so that I can store the value in a variable then move it to another part of the message.

      a side note… the data elements that are before and after the special characters will vary in length. The data element that is between the special chars will change in length.

      Ideas?

    Viewing 6 reply threads
    • Author
      Replies
      • #81811
        Levy Lazarre
        Participant

          Dustin,

          I think a regular expression may be the easiest way to do this.

          I would use the following regex and run the regexp command against the string:

          Code:



          set expr “.*?\^(.*?)_.*”

          A quick try at the Tcl command prompt. The string you are looking for will be stored in the second variable, var2:

          Code:



          % set mystring NA^MICRO_HUN

          NA^MICRO_HUN

          % set expr “.*?\^(.*?)_.*”

          .*?^(.*?)_.*

          % regexp $expr $mystring var1 var2

          1
          % puts $var2

          MICRO

          The regex is saying “Consume all characters up to the ^ in a non-greedy way (?), save all characters between the ^ and the _ in a non-greedy way, consume the rest of the characters.

          I hope this puts you on the right track.

        • #81812
          Jim Kosloskey
          Participant

            Is this in an Xlate?

            If so, what you show is 2 components of a field (assuming standard HL/7) so first off you can get the second component using ccomponent notation in the address path after the field # (like 0(0).(0)…OBX(0).#5(0) add [1] so 0(0).(0)…OBX(0).#5(0).[1]).

            That will give you ‘MICRO_HUN’. Now in my case I have a Tcl proc which will split a string based on any character so I woulld tell my proc to split on ‘_’ and put the first element split away (MICRO) into the Destination.

            If you want this proc, email me and I will send it to you.

            You can do your own Tcl if you like and in that code after getting the xlateInVals element that is the component (see above) use the Tcl split command which will give you two elements (MICRO and HUN)  put the first element of that list in the first element of XlateOutVals.

            I hope this helps.

            email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

          • #81813
            Dustin Sayes
            Participant

              Levy, Jim, thank you both for the proposed solutions.

              Jim, I like your approach. It is clean and simply makes sense, as usual!

              For this particular interface I have a fair amount of message translations going on. I had to leave the Xlate tool and move to tcl given the degree of modifications, so adding the regexp to my tps is my preferred method.

              Levy, thank you for posting the code for the regexp. I was thinking that a regexp would get the job done, but I simply couldn’t get it right. So thank you for your knowledge here. I’ll have to ask you Levy, is there a resource that you used to build the regexp?

              Again, thank you both. Both solutions are valid and usable!

              -Dustin

            • #81814
              Charlie Bursell
              Participant

                I agree with your solution Levy as long as your Tcl proc is doing other stuff.  Otherwise the solution provided by Jim would be best

                FWIW Jim the new Xlate has a lot of those string commands built-in so no need for a proc.

                As far as the regular expr (REGEXP) you should always put it inside brackets {} and not quotes unless there are variables in the expression that need to be evaluated.  If inside brackets the Tcl interpreter will not attempt to interpret the expression and just pass it to the regexp engine as-is.  No need for the extra backslash for instance and sometimes more unintended consequences

                set str NA^MICRO_HUN

                set val “”;# Always do this in case of no match

                regexp — {^.*^(.*?)_} $str {} val

                Note a couple of things:

                Always put the — before the rexexp so it knows there are no more options.  What if the regexp started with -?

                You don’t need the .* after the _ unless it plays a role in the match

                Note the first variable after $str returns the whole string that was matched.  Rather than wasting a variable on it send to bit bucket as a null {}.  This is also better documentation as anyone looking at the code knows you did not need that value.

                Lot’s of ways to do the ame thing in this business.  There are only two people that do it right, you and me, and sometimes I wonder about you  ðŸ˜€

              • #81815
                Jim Kosloskey
                Participant

                  Charlie,

                  Thanks for the information reegarding the new built-ins. I noted that new functionality when I was speaking with a Cloverleaf User who is on a later than 6.0.0 release.

                  I like that direction.

                  I have no idea when I will have the opportunty to experience that first hand but I would encourage the built-in functionality over home-grown Tcl and would gladly abandon whatever code I have in favor of such functionality.

                  email: jim.kosloskey@jim-kosloskey.com 29+ years Cloverleaf, 59 years IT - old fart.

                • #81816
                  Levy Lazarre
                  Participant

                    I agree with your refinements to the regex, Charlie, and I have used them in my production system.

                    Dustin, you could try Visual REGEXP (a free Tcl/Tk program) to assist you in your learning. It can be downloaded at

                    http://laurent.riesterer.free.fr/regexp/

                  • #81817
                    Bob Richardson
                    Participant

                      Greetings one and all,

                      Just a caution here on Integrators 6.0 thru 6.1:  the R & D fix to pass a string now instead of a list at Xlate time for a COPY action on a field containing components may come back to bite you.

                      We have had to fix some of our Xlates inline code (Tcl) to bypass this fix.

                      We are testing/evaluating Integrator 6.1.0 currently.

                      There is a conversion utiliity available from INFOR R & D (hcixltupdate)

                      that will plug in code to convert this string into the old former list.

                      Only designed for VRL input and HL7 (no HRL if you use this format for an input template).  And if your code begins with the lassign command on XlateInvals.

                      Enjoy.

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