拆分字符串并仅打印字母数字字符串中的数字

split a string and print only numbers from an alphanumeric string

我是Tcl的新手。我有一个特定的字符串“code_lines_part2021vol32i8mn.txt”,我只想打印格式为“2021 32 8”的数字我该怎么做?

提前致谢

你可以试试这个...想法是用 regsub command 删除除数字以外的所有字符并替换为 space :

set your_string "code_lines_part2021vol32i8mn.txt"
regsub -all -nocase {[._a-z]} $your_string { } newstring
lassign $newstring num_1 num_2 num_3
puts "result = $num_1 $num_2 $num_3"
# result = 2021 32 8

使用 regexp command 可能会更容易,但我还没有弄明白。
文档:regsub, lassign and regexp(如果你想尝试)

我没有旧的 Tcl 来测试,但试试这个:

set numbers [regexp -all -inline {\d+} $string]
puts [join $numbers]