tcl how to split a string by using regexp

Advertisements

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

>Solution :

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 match value is not necessary, we are only interested to get Group 1 value.

Leave a Reply Cancel reply