Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regsub question

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I have a string which contains a filename like
toto.bri.meta

I would like to remove the extension .bri.meta and end up with toto

I have tried regsub -all {.bri.meta} string {} stringout but this did not work, what should I do?

Thanks!
 
proc one_elem {str {mk "0"}} {
if {![catch {set mylist [split $str "."]}]} {
return [lindex $mylist $mk]
}
return 1
}

ex:
one_elem this.is.a.test
this
(mars) 54 % one_elem this.is.a.test 1
is
(mars) 56 % one_elem this.is.a.test 3
test
(mars) 57 % one_elem this.is.a.test 2
a
 
What about:
Code:
regexp {([^.]*)\.} toto.bri.meta - stringout
or
Code:
set stringout [lindex [split toto.bri.meta .] 0]
?

The reg expression means: all chars being not "." and followed by ".".
The split comand returns [list toto bri meta].

Good luck

ulis
 
Could you explain how your regsub command "didn't work"? I see two problems with the example you posted. Assuming that the input string is stored in the variable string, you need a "$" in front of the variable name to get its value:

[tt]regsub -all {.bri.meta} $string {} stringout[/tt]

There is also a slight problem with your regular expression, although it should still have worked for the most part. In a regular expression "." matches any single character. To match a literal ".", you need to escape it with a "\":

[tt]regsub -all {\.bri\.meta} $string {} stringout[/tt]

But for simple string substitutions like this (substituting constant string values), the string map command is more efficient and easier to use. Just make sure that you're using Tcl 8.1.1 or later, which is when the string map command was introduced. In this case, you could simply say:

Code:
set stringout [string map {.bri.meta {}} $string]
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
thanks very much for your replies guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top