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.
- 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:
128 + 0x80 = 256;
90 + 0x90 = 0xB4;
200 + 0x200 = 712;
- On the Match panel, set “begin regex match at” to “start of word”, and set “end regex match at” to “end of word”.
- Click the button to add field .
- In the “pattern to match field” drop-down list, select “integer”.
- In the list of allowed integer formats, check “decimal 987” and “hexadecimal 0x3DB”.
- Turn on “limit the integers to these ranges”.
- Enter 128..255 as the range to limit the integers to.
- Set the “field validation mode” to “strict”. This is necessary to make RegexMagic limit the integer range to exactly 128 and 255.
- 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:
\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.
- 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).
- 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:
1
16
42
174
2552
8088
80386
- 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.
- Select the number 42 in the sample text and click the Mark button.
- Select the number 2552 in the sample text and click the 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.
- Switch to the Match panel. You’ll notice RegexMagic correctly auto-detected the integer pattern with the range 42..2552.
- Click the “limit the integers to these ranges” checkbox to tell RegexMagic we want to use the minimum and maximum values it auto-detected.
- 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.
- 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:
\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.
- The Samples panel now highlights the integers between 42 and 2552:
1
16
42
174
2552
8088
80386
Related Examples
Reference