regex – TCL regexp match multiple target in 1 string probelm

To get all the matches in a string at once, you use the -all option to regexp, probably with -inline (unless you just want a count of how many matches there are). You either get the text of the matches (default), or the indices that they were found at (if you give the -indices option). You’d then usually use foreach to go over the list of match results; that’s an effective combination.

Here’s an example:

set inputText "badaafaaagaaaahaaaaa"
foreach aSequence [regexp -all -inline {a+} $inputText] {
    puts $aSequence
}

That prints:

a
aa
aaa
aaaa
aaaaa

Note that if you use capturing parentheses in your regular expression, you probably want the multi-variable form of foreach to go over the result.

Read more here: Source link