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.

  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:
    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
    
  3. 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.
  4. 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.
  5. Click the Add First Field button to add field 1.
  6. In the “pattern to match field” drop-down list, select “number”.



  7. 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.
  8. Set the “field validation mode” to “strict”. This is necessary to make RegexMagic limit the integer range to exactly 1 and 14.
  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:
    \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.

  10. 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:

  1. Change the “maximum value of integer part” from 14 to 13.
  2. Select “alternation” in the “kind of field” drop-down list for field 1. When you do this, RegexMagic adds field 2 as the first alternative under field 1, and sets the new field to use the “number” pattern that we configured previously.
  3. Click the Add Last Sub-Field button Add Last Sub-Field to add field 3 as the last field under field 1. The new field defaults to the same “number” pattern.
  4. 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 3 selected from the previous step. In the “pattern to match field” drop-down list, select “literal text”.
  5. Enter 14 in the text box for the “literal text” pattern for field 3.
  6. Click the Add Next Field button Add Next Field to add field 4 after field 3. The new field defaults to the same “literal text” pattern.
  7. Enter 14.0 in the text box for the “literal text” pattern for field 4.
  8. 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.

  9. 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 1 highlighting we had previously is gone. Field 1 is now an alternation field. Alternation fields don’t match any text directly, but simply alternate between several fields. In our formula, field 1 has three alternatives. Field 2 matches a number between 1.0 and 13.9. Field 3 matches “14”. Field 4 matches “14.0”. The highlighting on the Samples panel indicates which field matched which text.

Related Examples

Reference