replacing special characters

Clovertech Forums Read Only Archives Cloverleaf Tcl Library replacing special characters

  • Creator
    Topic
  • #51376
    Kevin Crist
    Participant

      I have been asked to remove any special characters from a certain field. I have done this with say removing just an “-” or something else, but never remove anything non-numeric.  So if it looks like this 9876543-21 i would want to remove the “-” and make it 98765421. Or something like this 125412*1414 to 1254121414. How would do something like this?.

      Thanks for any ideas.

    Viewing 9 reply threads
    • Author
      Replies
      • #69942

        Try this:

        Code:

        set var [string map -nocase {- {}} $var]

        Or from an xlate tcl fragment:

        Code:

        set var [string map -nocase {- {}} [lindex $xlateInVals 0]]; set xlateOutVals [list $var]

        -- Max Drown (Infor)

      • #69943
        Kevin Crist
        Participant

          Thanks Max, I didnt have any luck, it doesnt seem to change anything in that field. Here is what i have in the xlate fragment which i dont do very often so i may have it wrong

          lassign xlateInVals PolicyNum

          set PolicyNum [string map -nocase {- {}} [lindex $xlateInVals 0]

          set xlateOutVlas

            ]
        • #69944

          Here’s some sample code that seems to be working for me from a test tcl script (>tcl test.tcl). There was an error in the code above which I’ve fixed.

          Code:

          lappend xlateInVals “9876543-21”
          set var [string map -nocase {- {}} [lindex $xlateInVals 0]]
          set xlateOutVals [list $var]
          puts [lindex $xlateOutVals 0]
          #returns: 987654321

          -- Max Drown (Infor)

        • #69945
          Kevin Crist
          Participant

            ok, i got that…i misspelled a word, gets me everytime. Is thier a way to keep adding characters to the list other than the “-” to search for? Example @,$, A-z?

          • #69946

            Yes. the command takes “pairs” of character where the first is the character or string to look for and the second is the replacement.

            Ex. – {} * {} a b 1 2

            ({} means replace with nothing or remove)

            -- Max Drown (Infor)

          • #69947
            Kevin Crist
            Participant

              ok, i think i understand it now but i  have one more question…how do you replace letters…any and all. i have tried A-z, a-z, A-Z, and chars. Is their a special way to do all of them besides putting in a {} b {} c {} or abcde… {} or a wild card of some kind?

              Thanks so much for your help

            • #69948

              If you need to replace a range of characters, I believe you will need to use regsub instead of string map.

              Here’s an example:

              Code:

              lappend xlateInVals “abc-12*3”
              regsub -all -nocase {[a-z-*]} [lindex $xlateInVals 0] “” var; set xlateOutVals [list $var]
              puts [lindex $xlateOutVals 0]
              #returns: 123

              -- Max Drown (Infor)

            • #69949
              Tom Rioux
              Participant

                Kevin,

                One thing that may work for you may be by doing something like the following:

                hcitcl>set var 12345ABCDE6789

                12345ABCDE6789

                hcitcl>set rvar [join [regexp -all -inline {[A-Z]} $var] “”]

                ABCDE

                hcitcl>set newvar [string map -nocase “$rvar {}” $var]

                123456789

                Hope this helps…..

                Tom Rioux

              • #69950
                Steve Carter
                Participant

                  How about:

                  regsub -all — {[^0-9a-zA-Z]} $string “” string

                  This will remove anything from the string that isn’t alphanumeric.

                  Steve

                • #69951
                  Charlie Bursell
                  Participant

                    Steve:

                    If you are a lazy typist like I am  ðŸ˜€ , theis will do the same thing except it adds the underscore:

                    regsub -all — {W} $string {} string

                    would be equivalent to:

                    regsub -all — {[^0-9a-zA-Z_]} $string “” string

                Viewing 9 reply threads
                • The forum ‘Tcl Library’ is closed to new topics and replies.