$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.
- Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
- On the Samples panel, paste in one new sample:
$VARIABLE_NAME
${BETWEEN_BRACES}
$ANOTHER_VARIABLE
$VAR4
${VAR5}
$6
${7}
- 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.
- Select the first $ in the sample and click the Mark button. RegexMagic adds field and detects it should match a literal $.
- Select VARIABLE_NAME and click the Mark button. RegexMagic adds field and also detects it as literal text. We’ll fix that later.
- Select the second $ in the sample and click the 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.
- Select the { and click the Mark button. This tells RegexMagic that two different fields can follow field . To make this happen, RegexMagic changes field to be an alternation field. The field matching VARIABLE_NAME becomes field , the first alternative under field . The newly marked field for the opening brace is added as field , the second alternative under field .
- Select BETWEEN_BRACES and click the Mark button. This tells RegexMagic that the second alternative under field consists of two consecutive fields (so far): the brace and BETWEEN_BRACES. To make this happen, RegexMagic changes to be a sequence field. The field to match the brace becomes field , and the newly marked BETWEEN_BRACES becomes field .
- Select the } and click the Mark button. RegexMagic continues the sequence adding field .
- 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.
- On the Match panel, click on the big or select field in the “select field” drop-down list.
- In the “pattern to match field” drop-down list, select “basic characters”. RegexMagic automatically detects we want the underscore and uppercase letters.
- Tick the boxes for lowercase letters and digits to allow any alphanumeric sequence.
- Select field , change its pattern to “basic characters”, and tick “digits” and “lowercase letters”.
- In the tree of fields on the Match panel, set fields and to repeat between 1 and 32 times or whatever lengths your variable names are allowed to have.
- On the Regex panel, select “C# (.NET 2.0–8.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.
- 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