I want to find a pattern in a file, but the pattern can have several forms.
Here is the code :
while {[gets $thefile line] >= 0} {
for {set nb_table 1} {$nb_table<$count_table} {incr nb_table} {
if { [regexp {pattern_$nb_table} $line] } {
puts "I found one !"
}
}
}
the var $count_table is known, catched before on a on a other procedure.
If i do a puts of pattern_$nb_table in the for loop i got the name of all tables and that’s good, but I never have I found one! printed out (sure i want to be another process but it is not the subject). Why I never go in the if? My file contains the pattern : pattern_1 pattern_2 .....
The problem is that the variable is not being substituted into the regular expression (the
{…}disable all immediate substitutions). This is a situation where you’d use (putting the variable name in braces just for clarity, and putting the pattern in double quotes for highlighting only):Except that if I was looking for a string that simple, I’d try to use
string firstorstring match:Both of these are faster than regular expression matching, provided you’re doing something simple. If the rest of the real
patternis a regular expression, onlyregexpwill do. Of course.