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 8446971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:04:11+00:00 2026-06-10T10:04:11+00:00

I am trying to extract a substring from a string in Tcl. I wrote

  • 0

I am trying to extract a substring from a string in Tcl. I wrote the code and able to do it, but I was wondering if there is any other efficient way to do it. So the exact problem is I have a string

name_ext_10a.string_10a.string.string.string

and I want to extract “name_ext“, and then remove that “_” and replace it with “.“; I finally want the output to be “name.ext“. I wrote something like this:

set _File "[string replace $_File [string last "_" $_File] [string length $_File] "" ]"
set _File "[string replace $_File [string last "_" $_File] [string length $_File] "" ]"
set _File "[string replace $_File [string last "_" $_File] [string last "_" $_File] "." ]"

which gives me the exact output I want, but I was wondering if there is any other efficient way to do this in Tcl.

  • 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-10T10:04:12+00:00Added an answer on June 10, 2026 at 10:04 am

    You could split that filename using underscore as a separator, and then join the first 2 elements with a dot:

    % set f name_ext_10a.string_10a.string.string.string
    name_ext_10a.string_10a.string.string.string
    % set out [join [lrange [split $f _] 0 1] .]
    name.ext
    

    EDIT

    So if “name” can have an arbitrary number of underscores:

    set f "foo_bar_baz_ext_10a.string_10a.string.string.string"
    set pieces [split $f _]
    set name [join [lrange $pieces 0 end-3] _]
    set out [join [list $name [lindex $pieces end-2]] .]  ;#==> foo_bar_baz.ext
    

    But this is getting complex. One regex should suffice — I assume “string” can be any sequence of non-underscore chars.

    set string {[^_]+}
    set regex "^(.+)_($string)_10a.${string}_10a.$string.$string.$string\$"
    regexp $regex $f -> name ext
    set out "$name.$ext"    ;#==> foo_bar_baz.ext
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract substring from string gettting from mysql database using substr
I'm trying to extract a substring matching a given pattern from a string in
im trying to extract some files from a jar-file downloaded using java-webstart. below code
I'm trying to extract a substring in this format: preceeding-string-data-i-want Where: preceeding-string should be
I am trying extract some information from the below given string >>> st =
Im trying to extract a line from wget's result but having trouble with it.
I am trying to extract a date from a string variable and was hoping
I'm trying to extract the domain name from a string in C#. You don't
I am trying to extract a sub string from a string in python. My
While trying to extract data from a view by joining it with two other

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.