TCL – Using RegExp to Search on a String

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf TCL – Using RegExp to Search on a String

  • Creator
    Topic
  • #50955
    Derek Stukey
    Participant

      Hi All,

      I am trying to use a regexp to search on the string “SSS”.  If this string is found in a particular segment, then capture all characters which come BEFORE the “SSS” and save them to a variable.

      The issue is the fact that the regexp does not search on “SSS”, but rather just “S”.  So, if a capital S appears anywhere before the “SSS”, all of the logic is blown for the rest of the script.

      The string I am testing with is: General SisterSSS001ABC123 Information

      If the regexp works correctly, the appropriate output would be “Sister” to the caputuring variable.

      I have tried the following options and have not gotten the desired result:

      A.  regexp {(^[SSS]*)}

       A1.  Output: S

      B.  regexp {(^(SSS)*)}

       B1.  Output:

      C.  regexp {([^”SSS”]*)}

       C1.  Output:

      I have tried a few other variations, but in all cases, the regexp continues to focus on the first instance of “S” and not the “SSS”.  Does anyone know the syntax to isolate a string of the same letters using regexp?  Or, am I wasting my time?

      Thanks,

    Viewing 10 reply threads
    • Author
      Replies
      • #68172
        Ron Archambault
        Participant

          Tty this

          hcitcl>set a “SSS”

          hcitcl>regexp “^SSS” $a

          1

          hcitcl>regexp “^

          hcitcl>

          hcitcl>

          hcitcl>set b SBS

          SBS

          hcitcl>regexp “^SSS” $b

          0

        • #68173
          Derek Stukey
          Participant

            Hi Ron,

            Thanks for the response.  I am not entirely sure what you were aiming for here.  Do you think the code “^SSS” is what I should be using?

            Thanks,

            Derek

          • #68174
            Ron Archambault
            Participant

              Should work for you.

            • #68175
              Keith McLeod
              Participant

                How about something like:

                set x “General SisterSSS001ABC123 Information”

                regexp — {s+(.+)SSS.+} $x –> tst

                echo $tst

                hcitcl>set x “General SisterSSS001ABC123 Information”

                General SisterSSS001ABC123 Information

                hcitcl>regexp — {s+(.+)SSS.+} $x –> tst

                1

                hcitcl>echo $tst

                Sister

              • #68176
                Derek Stukey
                Participant

                  Hi Keith,

                  I will test this out and let you know.

                  Thanks for your help,

                  Derek

                • #68177
                  Jim Kosloskey
                  Participant

                    Unless you just want to practice unraveling all of the secret handshakes of regexp, you could do this:

                    hcitcl>set junk “General SisterSSS001ABC123 Information”

                    General SisterSSS001ABC123 Information

                    hcitcl>set first [string first “SSS” $junk]

                    14

                    hcitcl>set stuff [string range $junk 0 [expr $first – 1]]

                    General Sister

                    The expr is because the index from the string first points to the first S of the SSS.

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

                  • #68178
                    Charlie Bursell
                    Participant

                      What if the string is changed to: “SisterSSS001ABC123 Information” or what if there are 2 or three words before Sister?  I would rather do a two step operation.  Also I beleive the requirement was to capture only if the SSS were there

                      set x “General SisterSSS001ABC123 Information”

                      if {[regexp — {^(.*?)SSS.*?} $x {} tst]} [

                          set val [lindex $tst end]

                      } else {

                          set val “”

                      }

                      Never trust anyone with a last name like Archambault!  ðŸ˜€

                    • #68179
                      Ron Archambault
                      Participant

                        I wouldn’t. Especially since I tell everyone that I learned most of my tricks from you Mon Ami!

                        :-).

                      • #68180
                        Derek Stukey
                        Participant

                          Hi Charlie,

                          The code worked!  Two follow up questions if you don’t mind:

                          1.  At the end of the regexp, you use $variable {} variable

                          What is the {} used for?

                          2.  Earlier in this post, Keith McLeod used $variable –> tst (via command line example).

                          When I ran the code with –> in the command line, it returned the correct result.  When I ran the code in the Testing Tool via TPS test, it did not work.

                          A.  What does –> do?

                          B.  Is there a way to get it to work within Cloverleaf?

                          Thanks again for your help,

                          Derek

                        • #68181
                          Charlie Bursell
                          Participant

                            Both the {} and -> in Kieth’s case do basically the same thing.

                            In the regexp command the first variable, {} or -> in this case, hold the whole string that matched while subsequent variables hold the value of the contents ofthe parenthesis starting from let to right.

                            In my case I sent the entire match to an empty string (bit bucket).  In Kieth’s case he stored it to a meaningless value named ->.  You may sometimes see this a “match” or other names.  It depends on whther you need it or not.  We did not.

                            I hope this helps

                          • #68182
                            Derek Stukey
                            Participant

                              Got it.  Much more understanding still needed for the future!

                              Much thanks to Charlie B., Ron A., Jim K., and Keith M. for your assistance.  I got my code to finally work!

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