1

I am new to regex and still trying to wrap my head around it. I am stuck at this one point and all related questions to my problem doesnt seem to help.

I have a text varible

set text "/folders/beta_0_2_1"

I want to extract 0, 2, 1 and save them in three different variables using regex in tcl. I tried to reach through beta variable using

[regexp {/beta_} $text]

However, I am not able to figure out the part where I can extract each of those variables and then save them. Can you provide me some direction?

2 Answers 2

4

You can use it like this:

regexp {/beta_([0-9]+)_([0-9]+)_([0-9]+)} $text -> num1 num2 num3

Then you can use the variables:

puts "$num1 $num2 $num3"
# => 0 2 1

-> by the way is a convention. This variable (yes, it's one!) will contain the whole match.


And as a side note, you could also split it by underscore:

lassign [split $text "_"] - num1 num2 num3

puts "$num1 $num2 $num3"
# => 0 2 1
Sign up to request clarification or add additional context in comments.

1 Comment

@UnderDog Yup! It's used with lists and was introduced in Tcl 8.5.
3

to extract all the sequences of digits:

% set text "/folders/beta_0_2_1"
/folders/beta_0_2_1
% regexp -inline -all {\d+} $text
0 2 1
% lassign [regexp -inline -all {\d+} $text] a b c
% puts $a; puts $b; puts $c
0
2
1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.