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?

+3


source to share


3 answers


Use the expression below and get the version number from group index 1.

Version\s*([\d.]+)

      

DEMO



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

      

+10


source


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.]+

      

Online demo



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)

      

+1


source


(?<=Version\s)\S+

      

Try it. Use it with help re.findall

.

x="""MySQL-vm
  Version 1.0.1

  WARNING:: NEVER EDIT/DELETE THIS SECTION"""

print re.findall(r"(?<=Version\s)\S+",x)

      

Output: ['1.0.1']

See demo.

http://regex101.com/r/dK1xR4/12

0


source







All Articles