Regular expression capture variables

Within conditional statements and node definitions, substrings withing parentheses () in a regular expression are available as numbered variables inside the associated code section. The first is $1, the second is $2, and so on. The entire match is available as $0.

These are not normal variables, and have some special behaviors:

  • The values of the numbered variables do not persist outside the code block associated with the pattern that set them.

  • You can’t manually assign values to a variable with only digits in its name; they can only be set by pattern matching.

  • In nested conditionals, each conditional has its own set of values for the set of numbered variables. At the end of an interior statement, the numbered variables are reset to their previous values for the remainder of the outside statement. This causes conditional statements to act like local scopes, but only with regard to the numbered variables.