Pattern: Number
“Number” is one of the patterns that you can select on the Match panel. Use this pattern to make a field match a decimal number that may or may not have decimals, thousand separators, currency symbols, signs, exponents, etc.
This example shows how you can use the “number” pattern to make your regular expression match a pH values. pH values range from 1.0 to 14.0. For this example, we’ll allow one optional decimal. You can find this example as “Pattern: number (basic)” 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:
Valid pH values:
1
1.0
2.9
4
7.2
10.4
14
14.0
Invalid pH values:
-7.2
-4
.8
0.9
14.1
15.1
- Set the subject scope to “line by line” to tell RegexMagic to treat each line in our sample as if it were a separate sample. This way we don’t have to provide each sample number separately.
- On the Match panel, set “begin regex match at” to “start of text”, and set “end regex match at” to “end of text”. We want to create a regex that only matches strings that consist of nothing but a pH number.
- Click the button to add field .
- In the “pattern to match field” drop-down list, select “number”.
- Configure the number pattern as in the screen shot: integer range limited from 1 to 14, period as the decimal separator, and one optional decimal.
- Set the “field validation mode” to “strict”. This is necessary to make RegexMagic limit the integer range to exactly 1 and 14.
- 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:
\A(?:1[0-4]|[1-9])(?:\.[0-9])?\z
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 which numbers our regular expression matches:
Valid pH values:
1
1.0
2.9
4
7.2
10.4
14
14.0
Invalid pH values:
-7.2
-4
.8
0.9
14.1
15.1
Number with Literal Text Alternatives
The regular expression we just created isn’t perfect. 14.1 is not a valid pH value. The problem is that the “number” pattern in RegexMagic only has options to limit the range of the integer part, and to limit the number of decimal digits. It doesn’t have an option to limit the decimal part to specific values, so all we could do was to configure the pattern to match numbers between 1 and 14 with one decimal, resulting in a range from 1.0 to 14.9.
We can solve this problem by limiting the number pattern to a range from 1.0 to 13.9. We can then add alternatives to make our regular expression match the 14 and 14.0 values. The “Pattern: number (improved)” example in the RegexMagic library has the final result. These steps create the same if you continue from the above steps:
- Change the “maximum value of integer part” from 14 to 13.
- Select “alternation” in the “kind of field” drop-down list for field . When you do this, RegexMagic adds field as the first alternative under field , and sets the new field to use the “number” pattern that we configured previously.
- Click the Add Last Sub-Field button to add field as the last field under field . The new field defaults to the same “number” pattern.
- Though we could use the “number” pattern to match “14”, it’s much easier to do this with the “literal text” pattern. The “select field” drop-down list should still have field selected from the previous step. In the “pattern to match field” drop-down list, select “literal text”.
- Enter 14 in the text box for the “literal text” pattern for field .
- Click the Add Next Field button to add field after field . The new field defaults to the same “literal text” pattern.
- Enter 14.0 in the text box for the “literal text” pattern for field .
- Make sure the Generate button is still pressed on the Regex panel and you’ll get this regular expression:
\A(?:(?:1[0-3]|[1-9])(?:\.[0-9])?|14|14\.0)\z
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 regular expression correctly matches pH values from 1.0 to 14.0:
Valid pH values:
1
1.0
2.9
4
7.2
10.4
14
14.0
Invalid pH values:
-7.2
-4
.8
0.9
14.1
15.1
You’ll notice that three different colors now appear on the Samples panel. The yellow field highlighting we had previously is gone. Field is now an alternation field. Alternation fields don’t match any text directly, but simply alternate between several fields. In our formula, field has three alternatives. Field matches a number between 1.0 and 13.9. Field matches “14”. Field matches “14.0”. The highlighting on the Samples panel indicates which field matched which text.
Related Examples
Reference