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.
- 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:
v6.0.156 for Windows 2000/2003/XP/Vista
Server version 1.1.20
Client Manager version 1.1.24
v12.22.5.1333
- Select the v at the start of the sample and click the Mark button. RegexMagic adds field which matches a literal v.
- On the Match panel, set the “repeat this field” spinner controls for field to 0 and 1 in order to make this field optional.
- Back on the Samples panel, select the 6 immediately after the v we just marked and click the Mark button. RegexMagic adds field which matches a decimal integer.
- Select the dot immediately after the 6 and click the Mark button. RegexMagic adds field which matches a literal dot.
- Select the 0 immediately after the dot we just marked and click the Mark button. RegexMagic adds field 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.
- Select the number 22 in the last line of the sample and click button above the samples. Now RegexMagic changes field to match a decimal integer.
- Select the dot after the 22 we just marked and click the Mark button. RegexMagic adds field which matches a literal dot.
- Mark the number 5 in the last line of the sample and click the Mark button. RegexMagic adds field which matches a decimal integer.
- Mark the dot after the number 5 and click the Mark button. RegexMagic adds field to match the literal dot.
- 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 . RegexMagic moves the old field that matches the literal dot as field under the new sequence field .
- Set the “repeat this field” spinner controls for field to 0 and 1 in order to make this sequence field optional.
- Select the number 1333 at the end of the sample text and click the Mark button. RegexMagic adds field as the second field under sequence field and configures it to match an integer.
- 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:
\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.
- 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.
- On the Action panel, click the New button to add a capturing group.
- 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.
- Select “ integer” in the “field” drop-down list. We now have a capturing group that will give us the major version number.
- Click the New button, enter “minor” as the group’s name, and select field to add a capturing group for the minor version number.
- Click the New button, enter “release” as the group’s name, and select field to add a capturing group for the release number.
- Click the New button, enter “build” as the group’s name, and select field to add a capturing group for the build number.
- 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.
- 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: , , , and . 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