Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Find the version number
MoyenStrings10 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.
Exemples
extractVersion('oracle release 12.2c')Renvoie '12.2'
extractVersion('Python 3.10.4 released')Renvoie '3.10.4'
extractVersion('no version')Renvoie null
Posé chez Oracle · Source
Chargement de l'éditeur...
- Entrée
extractVersion('oracle release 12.2c')Attendu'12.2' - Entrée
extractVersion('Python 3.10.4 released')Attendu'3.10.4' - Entrée
extractVersion('no version')Attendunull
Les tests cachés se lancent à la soumission