Search code examples
tcl

What do these characters at the beginning of Tcl lists on multiple lines mean?


Excuse me this beginner question: I can't understand these characters at the beginning of Tcl list declarations spanning on multiple lines. Like at the end of the first line of this code snippet:

set word_rx {(?x)
          [0-9]+
        | [a-z]+  
        | [a-z]+'[a-z]+
    }

I have also seen examples of an ampersand being used there. What do they mean and are there other ones?


Solution

  • That's a regular expression, not anything meant to be treated as a list. The pipes are the usual regular expression alternation operator. Maybe the expanded syntax option is throwing you off?

    (?x)
      [0-9]+
    | [a-z]+  
    | [a-z]+'[a-z]+
    

    is the same as

    [0-9]+|[a-z]+|[a-z]+'[a-z]+
    

    but more readable because the alternations are split up onto multiple lines.

    The braces, of course, just treat what's in them as literal text so what's in the brackets aren't subject to command substitution.