$VAR and ${VAR} Variable Names

Some programming languages allow variable names to be specified as $VAR or ${VAR} where VAR is an alphanumeric sequence. This example shows how you can generate a regular expression to match any variable in either notation. You can find it as “Real world: $var and ${var} variable names” in the RegexMagic library.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    $VARIABLE_NAME
    ${BETWEEN_BRACES}
    $ANOTHER_VARIABLE
    $VAR4
    ${VAR5}
    $6
    ${7}
  3. On the Match panel, set both “begin regex match at” and “end regex match at” to “anywhere”. You can’t use start/end of word because $ and } are not word characters.
  4. Select the first $ in the sample and click the Mark button. RegexMagic adds field 1 and detects it should match a literal $.
  5. Select VARIABLE_NAME and click the Mark button. RegexMagic adds field 2 and also detects it as literal text. We’ll fix that later.
  6. Select the second $ in the sample and click the 1 button above the sample. This tells RegexMagic that the second dollar sign in the sample starts a variable, just like the $ we already marked does.
  7. Select the { and click the Mark button. This tells RegexMagic that two different fields can follow field 1. To make this happen, RegexMagic changes field 2 to be an alternation field. The field matching VARIABLE_NAME becomes field 3, the first alternative under field 2. The newly marked field for the opening brace is added as field 4, the second alternative under field 2.
  8. Select BETWEEN_BRACES and click the Mark button. This tells RegexMagic that the second alternative under field 2 consists of two consecutive fields (so far): the brace and BETWEEN_BRACES. To make this happen, RegexMagic changes 4 to be a sequence field. The field to match the brace becomes field 5, and the newly marked BETWEEN_BRACES becomes field 6.
  9. Select the } and click the Mark button. RegexMagic continues the sequence adding field 7.
  10. If you were to generate the regex now, it would correctly match the two variables we marked, but not the others. What’s left to do now is to tell RegexMagic that we want to allow any alphanumeric sequence rather than only VARIABLE_NAME and BETWEEN_BRACES.
  11. On the Match panel, click on the big 3 or select field 3 in the “select field” drop-down list.
  12. In the “pattern to match field” drop-down list, select “basic characters”. RegexMagic automatically detects we want the underscore and uppercase letters.
  13. Tick the boxes for lowercase letters and digits to allow any alphanumeric sequence.
  14. Select field 6, change its pattern to “basic characters”, and tick “digits” and “lowercase letters”.
  15. In the tree of fields on the Match panel, set fields 3 and 6 to repeat between 1 and 32 times or whatever lengths your variable names are allowed to have.
  16. On the Regex panel, select “C# (.NET 2.0–7.0)” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    \$(?:[0-9A-Z_a-z]{1,32}|\{[0-9A-Z_a-z]{1,32}\})

    Unused options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture.

  17. The Samples panel now shows that our regex matches all the version numbers:
    $VARIABLE_NAME
    ${BETWEEN_BRACES}
    $ANOTHER_VARIABLE
    $VAR4
    ${VAR5}
    $6
    ${7}

Essentially what we’ve done here is to separately mark all the different pieces of the text we’re trying to match so that we can use one RegexMagic Pattern for each of those pieces. Though it takes quite a number of steps, they’re all very straightforward. Once you get the hang of how RegexMagic combines patterns using alternation field and sequence fields you’ll be able to create your own regular expressions this way very quickly.

Related Examples

Reference