Pattern: Basic characters

“Basic characters” is one of the patterns that you can select on the Match panel. Use this pattern to restrict a field to a certain set of ASCII characters. The repetition settings for the field determine how many characters the field can or must match.

This example shows how you can use the “basic characters” pattern to match an identifier in a hypothetical programming language. This language allows identifiers that consist of any combination of letters, digits, dollar signs, and underscores. You can find this example as “Pattern: basic characters” 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:
    $first_and_second = $first + $second
  3. On the Match panel, set both “begin regex match at” and “end regex match at” to “anywhere”.
  4. Click the Add First Field button to add field 1.
  5. Tick the “unlimited” checkbox to allow field 1 to match one or more characters.
  6. In the “pattern to match field” drop-down list, select “basic characters”.



  7. Tick the “lowercase letters”, “uppercase letters”, and “digits” checkboxes to allow the pattern to match all ASCII letters and digits.
  8. Type a dollar sign and an underscore into the “individual characters” box to allow these two characters as well, without allowing any other punctuation.
  9. On the Regex panel, select “C# (.NET 2.0–4.8 & .NET Core 1.0–3.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]+

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

  10. The Samples panel now shows the regular expression finds 3 different identifiers in our sample text:
    $first_and_second = $first + $second

Related Examples

Reference