2

I have a string, I want to check if any strings from an array is a part of it or not:

set name abcxyz
set array [list abc efg hij]
set List [join $array "|"]
if {[regexp {($List)} $name]} {
    ...
} 

I don't want to use foreach loop is because there are some other condition in the if statement, and each will require a foreach loop. Then the run time would increase a lot.

Any help is appreciated!

1 Answers1

4

You're passing it as {($List)}. The curly braces {} means do not perform substitution. So your regex pattern is literally:

($List)

which is kind of silly since you're trying to match the word "List" after the end of the string which by definition should contain no more characters.

What you want instead is to pass it as "($List)". Double quotes "" mean perform substitution. So if you do:

regexp "($List)" $name

your regex pattern will be

(abc|efg|hij)

which is probably what you want.

slebetman
  • 109,858
  • 19
  • 140
  • 171