Matching Version Numbers

This example shows how you can generate a regular expression to match version numbers as they’re often used in the software world. The version number v1.2.3.4444 consists of a major version number, minor version number, release number, and build number. To make things interesting, we’ll allow for the v and the build number to be optional. When the build number is omitted, the dot that separates it from the release number must also be omitted. You can find this example as “Real world: version number (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:
    v6.0.156 for Windows 2000/2003/XP/Vista
    Server version 1.1.20
    Client Manager version 1.1.24
    v12.22.5.1333
  3. Select the v at the start of the sample and click the Mark button. RegexMagic adds field 1 which matches a literal v.
  4. On the Match panel, set the “repeat this field” spinner controls for field 1 to 0 and 1 in order to make this field optional.
  5. Back on the Samples panel, select the 6 immediately after the v we just marked and click the Mark button. RegexMagic adds field 2 which matches a decimal integer.
  6. Select the dot immediately after the 6 and click the Mark button. RegexMagic adds field 3 which matches a literal dot.
  7. Select the 0 immediately after the dot we just marked and click the Mark button. RegexMagic adds field 4 which matches a binary integer. That’s not what we want. We could change the pattern directly on the Match panel, or we could mark another sample to give RegexMagic more information for its auto-detection.
  8. Select the number 22 in the last line of the sample and click button 4 above the samples. Now RegexMagic changes field 4 to match a decimal integer.
  9. Select the dot after the 22 we just marked and click the Mark button. RegexMagic adds field 5 which matches a literal dot.
  10. Mark the number 5 in the last line of the sample and click the Mark button. RegexMagic adds field 6 which matches a decimal integer.
  11. Mark the dot after the number 5 and click the Mark button. RegexMagic adds field 7 to match the literal dot.
  12. Now, we want to make that dot optional, but in combination with the build number that follows it. To make two fields optional as a combination, we have to use a sequence field. On the Match panel, select “sequence” in the “kind of field” drop-down list for field 7. RegexMagic moves the old field 7 that matches the literal dot as field 8 under the new sequence field 7.
  13. Set the “repeat this field” spinner controls for field 7 to 0 and 1 in order to make this sequence field optional.
  14. Select the number 1333 at the end of the sample text and click the Mark button. RegexMagic adds field 9 as the second field under sequence field 7 and configures it to match an integer.
  15. 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:
    \bv?[0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?\b

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

  16. The Samples panel now shows that our regex matches all the version numbers:
    v6.0.156 for Windows 2000/2003/XP/Vista
    Server version 1.1.20
    Client Manager version 1.1.24
    v12.22.5.1333

Capturing Parts of Version Numbers

Sometimes, it’s not enough to just get the whole version number. You need to get the major, minor, release, and build numbers separately. To do that, we need to add capturing groups to the regular expression. This example continues from the previous one. You can find it as “Real world: version number (capture)” in the library.

  1. On the Action panel, click the New button to add a capturing group.
  2. Enter “major” as the group’s name. You should give your capturing groups a name even if your regex flavor does not support named capture.
  3. Select “2 integer” in the “field” drop-down list. We now have a capturing group that will give us the major version number.
  4. Click the New button, enter “minor” as the group’s name, and select field 4 to add a capturing group for the minor version number.
  5. Click the New button, enter “release” as the group’s name, and select field 6 to add a capturing group for the release number.
  6. Click the New button, enter “build” as the group’s name, and select field 9 to add a capturing group for the build number.
  7. On the Regex panel, click the Generate button if it isn’t down already to get the regular expression with added capturing groups:
    \bv?(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<release>[0-9]+)(?:\.(?<build>[0-9]+))?\b

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

  8. The matches highlighted on the Samples panel are still the same:
    v6.0.156 for Windows 2000/2003/XP/Vista
    Server version 1.1.20
    Client Manager version 1.1.24
    v12.22.5.1333

Matching Specific Version Numbers

Editing either of the above regular expressions to match only specific version numbers is trivial with RegexMagic. On the Match panel, we have defined four fields that use the integer pattern to match any decimal number: 2, 4, 6, and 9. You can easily set minimum and maximum values for one or more of these fields to match a specific range of version numbers.

Related Examples

Reference