Pattern: Integer

“Integer” is one of the patterns that you can select on the Match panel. Use this pattern to make a field match an integer number in decimal, hexadecimal, octal, and/or binary notation.

This example shows how you can use the “integer” pattern to look for an integer between 128 and 255 in either decimal or hexadecimal notation in a C-style (C#, Java, etc.) source code file. You can find this example as “Pattern: integer (match)” in the RegexMagic library. You can also watch a video of this example on RegexMagic’s website.

  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:
    128 + 0x80 = 256;
    90 + 0x90 = 0xB4;
    200 + 0x200 = 712;
    
  3. On the Match panel, set “begin regex match at” to “start of word”, and set “end regex match at” to “end of word”.
  4. Click the Add First Field button to add field 1.
  5. In the “pattern to match field” drop-down list, select “integer”.



  6. In the list of allowed integer formats, check “decimal 987” and “hexadecimal 0x3DB”.
  7. Turn on “limit the integers to these ranges”.
  8. Enter 128..255 as the range to limit the integers to.
  9. Set the “field validation mode” to “strict”. This is necessary to make RegexMagic limit the integer range to exactly 128 and 255.
  10. 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:
    \b(?:25[0-5]|2[0-4][0-9]|1[3-9][0-9]|12[89]|0x[89a-f][0-9a-f])\b

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

  11. The Samples panel now highlights the integers between 128 and 255 in decimal and hexadecimal notation:
    128 + 0x80 = 256;
    90 + 0x90 = 0xB4;
    200 + 0x200 = 712;
    

Marking Samples

Instead of using the Match panel to specify the details of the integer pattern as we did in the above example, we can use the Samples panel to mark the integers we’re interested in, and let RegexMagic’s auto-detection do the work. For this second example, we’ll generate a regular expression that matches any decimal integer between 42 (the answer to everything) and 2552 (the year on the Buddhist calendar that saw the release of RegexMagic 1.0).

  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:
    1
    16
    42
    174
    2552
    8088
    80386
  3. Select the text you just pasted in, and click the Unmark button on the Samples panel. This removes all fields from the Match panel that may be left over from a previous regular expression.
  4. Select the number 42 in the sample text and click the Mark button.
  5. Select the number 2552 in the sample text and click the 1 button. It’s important to use this button instead of the Mark button, because we want to provide an additional sample for the same field instead of adding a new field.
  6. Switch to the Match panel. You’ll notice RegexMagic correctly auto-detected the integer pattern with the range 42..2552.
  7. Click the “limit the integers to these ranges” checkbox to tell RegexMagic we want to use the minimum and maximum values it auto-detected.
  8. Set the “field validation mode” to “strict”. This is necessary to make RegexMagic limit the integer between the minimum and maximum values exactly, instead of merely restricting the number of digits.
  9. 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:
    \b(?:255[0-2]|25[0-4][0-9]|2[0-4][0-9]{2}|1[0-9]{3}|[1-9][0-9]{2}|[5-9][0-9]|4[2-9])\b

    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 highlights the integers between 42 and 2552:
    1
    16
    42
    174
    2552
    8088
    80386

Related Examples

Reference