Code Lab

Find the version number

MediumStrings10 min

Write a function extractVersion(text) that finds the first version number in a string and returns it. For 'oracle release 12.2c' the answer is '12.2'.

  • A version is one or more digits, then zero or more groups of a dot and one or more digits: 19, 12.2, 3.10.4.
  • Letters and other characters around the version do not matter: in 'v2.0', '19c' and '4.5-beta1' the versions are '2.0', '19' and '4.5'.
  • A dot with no digit right after it is not part of the version: for 'version 12.' return '12'.
  • If there are several versions, return the first one.
  • Return the version as a string, exactly as it is written in the text ('3.10' stays '3.10', not a number).
  • If the text has no digits (or is empty), return null.

A regular expression with match finds the version in one step. Remember that match returns null when nothing is found.

Examples

extractVersion('oracle release 12.2c')

Returns '12.2'

extractVersion('Python 3.10.4 released')

Returns '3.10.4'

extractVersion('no version')

Returns null

Asked at Oracle · Source

Loading the editor...
  1. InputextractVersion('oracle release 12.2c')
    Expected'12.2'
  2. InputextractVersion('Python 3.10.4 released')
    Expected'3.10.4'
  3. InputextractVersion('no version')
    Expectednull

Hidden tests run on Submit.