How do I extract a substring using tcl

Hello, I need to extract a substring from another string using tcl.

I have tried using regexp without success.

Here is an example of what I need to do:

string1 = "22/OBGJ/123456_5.66:22/OBGJ/000123_7.134"

result

"22/OBGJ/123456_5.66"

I need to extract the entire substring upto but NOT including the ":" character.

Is there a simple solution for this?

Much thanks!
LVL 3
dongjinkimAsked:
Who is Participating?
 
slyongConnect With a Mentor Commented:

set x "22/OBGJ/123456_5.66:22/OBGJ/000123_7.134"   ;# This is your string
puts "[string range $x 0 [expr [string first ":" $x] - 1]]"        ;# [string first ":" $x] gives you the index of the first ":" and use [expr theposition - 1] to get the index - 1 position, then string range just extract out the substring
0
 
JJSmithCommented:

echo $string1 | cut -d":" -f1



cheers
JJ
0
 
dongjinkimAuthor Commented:
I should clarify that this is for tcl, not shell scripting.
I was not able to find a Tcl related channel, I apologize if this is not the best place to pose the question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.