Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:34:27+00:00 2026-06-12T04:34:27+00:00

i am trying to replace all the special characters including white space, hyphen, etc,

  • 0

i am trying to replace all the special characters including white space, hyphen, etc, to underscore, from a string variable in tcl.

Example

    input: Stack Overflow helps%me(a lot
    output: Stack_Overflow_helps_me_a_lot

I wrote the code below but it doesn’t seem to be working.

    set varname $origVar
    puts "Variable Name :>> $varname"
    if {$varname != ""} {
        regsub -all {[\s-\]\[$^?+*()|\\%&#]} $varname "_" $newVar
    }
    puts "New Variable :>> $newVar"

one issue is that, instead of replacing the string in $varname, it is deleting the data after first space encountered inside $origVar instead of $varname. Also there is no value stored in $newVar. No idea why, and also i read the example code (for proper syntax) in my tcl book and according to that it should be something like this

    regsub -all {[\s-][$^?+*()|\\%&#]} $varname "_" newVar

so i used the same syntax but it didn’t work and gave the same result as modifying the $origVar instead of required $varname value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:34:28+00:00Added an answer on June 12, 2026 at 4:34 am

    The second version is closer. By putting the result in $newVar you’re actually setting a variable named whatever is stored in $newVar, i.e. you would then have to access it by $$newVar, so to speak (not valid in Tcl). Anyway, the issue is probably that in your second version, you don’t seem to be escaping certain characters. Try this:

    regsub -all {[\s\-\]\[$^?+*()|\\%&#]} $varname "_" newVar
    

    Another way to organize that to minimize escaping is this:

    regsub -all {[][\s$^?+*()|\\%&#-]} $varname "_" newVar
    

    And, I don’t know if this is too general, but you can try this too:

    regsub -all {\W} $varname "_" newVar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replace all the carriage return characters in a string obtained from
I'm trying to replace all _ underscore character by - hyphen character in all
I am trying to replace all German special characters in a Regular Expression. The
I'm trying to replace all the occurrences of a variable in a string using
I would like a RegExp that will remove all special characters from a string.
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
I'm trying to use a series of string.replaceAlls to swap all the UTF-8 special
I'm trying to replace all instances of   with a simple space EXCEPT if
I'm trying to replace all commas with a comma and a space. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.