Home > Software design >  tcl how to split a string by using regexp
tcl how to split a string by using regexp

Time:01-18

I have some string with format

class(amber#good)
class(Back1#notgood)
class(back#good)

and I want to use regexp to get value of these string

Expected answer:

amber
Back1
back

And here's my cmd:

set string "class(amber#good)"
regexp -all {^\\([a-zA-z_0-9].\#$} $string $match
puts $match

But the answer is not what I expected

CodePudding user response:

You can use

regexp {\(([^()#] )} $string - match

See the Tcl demo online.

The \(([^()#] ) regex matches

  • \( - a ( char
  • ([^()#] ) - Capturing group 1 (match): any one or more chars other than parentheses and #.

The hyphen is used since the whole-match value is not necessary, we are only interested to get Group 1 value.

  • Related