I agree with your solution Levy as long as your Tcl proc is doing other stuff. Otherwise the solution provided by Jim would be best
FWIW Jim the new Xlate has a lot of those string commands built-in so no need for a proc.
As far as the regular expr (REGEXP) you should always put it inside brackets {} and not quotes unless there are variables in the expression that need to be evaluated. If inside brackets the Tcl interpreter will not attempt to interpret the expression and just pass it to the regexp engine as-is. No need for the extra backslash for instance and sometimes more unintended consequences
set str NA^MICRO_HUN
set val “”;# Always do this in case of no match
regexp — {^.*^(.*?)_} $str {} val
Note a couple of things:
Always put the — before the rexexp so it knows there are no more options. What if the regexp started with -?
You don’t need the .* after the _ unless it plays a role in the match
Note the first variable after $str returns the whole string that was matched. Rather than wasting a variable on it send to bit bucket as a null {}. This is also better documentation as anyone looking at the code knows you did not need that value.
Lot’s of ways to do the ame thing in this business. There are only two people that do it right, you and me, and sometimes I wonder about you 😀