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.
v6.0.156 for Windows 2000/2003/XP/Vista Server version 1.1.20 Client Manager version 1.1.24 v12.22.5.1333
which matches a literal v.
to 0 and 1 in order to make this field optional.
which matches a decimal integer.
which matches a literal dot.
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.
above the samples. Now RegexMagic changes field
to match a decimal integer.
which matches a literal dot.
which matches a decimal integer.
to match the literal dot.
. RegexMagic moves the old field
that matches the literal dot as field
under the new sequence field
.
to 0 and 1 in order to make this sequence field optional.
as the second field under sequence field
and configures it to match an integer.
\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.
v6.0.156 for Windows 2000/2003/XP/Vista Server version 1.1.20 Client Manager version 1.1.24 v12.22.5.1333
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.
integer” in the “field” drop-down list. We now have a capturing group that will give us the major version number.
to add a capturing group for the minor version number.
to add a capturing group for the release number.
to add a capturing group for the build number.
\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.
v6.0.156 for Windows 2000/2003/XP/Vista Server version 1.1.20 Client Manager version 1.1.24 v12.22.5.1333
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.