Code Lab

Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado

Find the version number

MediaStrings10 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.

Ejemplos

extractVersion('oracle release 12.2c')

Devuelve '12.2'

extractVersion('Python 3.10.4 released')

Devuelve '3.10.4'

extractVersion('no version')

Devuelve null

Preguntada en Oracle · Fuente

Cargando el editor…
  1. EntradaextractVersion('oracle release 12.2c')
    Esperado'12.2'
  2. EntradaextractVersion('Python 3.10.4 released')
    Esperado'3.10.4'
  3. EntradaextractVersion('no version')
    Esperadonull

Las pruebas ocultas se ejecutan al enviar