Python regex to extract version from string
The line looks like this: ( \n
used for line break)
MySQL-vm
Version 1.0.1
WARNING:: NEVER EDIT/DELETE THIS SECTION
I only want 1.0.1.
I try re.search(r"Version+'([^']*)'", my_string, re.M).group(1)
but it doesn't work.
re.findall(r'\d+', version)
gives me an array of numbers which I have to add again.
How can I improve the regex?
source to share
Use the expression below and get the version number from group index 1.
Version\s*([\d.]+)
>>> import re
>>> s = """MySQL-vm
... Version 1.0.1
...
... WARNING:: NEVER EDIT/DELETE THIS SECTION"""
>>> re.search(r'Version\s*([\d.]+)', s).group(1)
'1.0.1'
Explanation:
Version 'Version'
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times)
( group and capture to \1:
[\d.]+ any character of: digits (0-9), '.' (1
or more times)
) end of \1
source to share
You can try "Positive Look" to not consume characters in the string, but only assert whether a match is possible or not. In the bottom regex you don't need the findAll
and functions group
.
(?<=Version )[\d.]+
Explanation:
(?<= look behind to see if there is:
Version 'Version '
) end of look-behind
[\d.]+ any character of: digits (0-9), '.' (1 or more times)
source to share