Home > other >  regex to match version number
regex to match version number

Time:09-13

hi everyone i have data parsed that i want to match.

i have list two strings i have parsed with:

        technologytitle=technologytitle.lower()
        vulntitle=vulntitle.lower()
        ree1=re.split(technologytitle, vulntitle)

This produces the following:

['\nmultiple cross-site scripting (xss) vulnerabilities in', '9.0.1 and earlier\n\n\n\n\n']

I am now trying to formulate writing re.match to match the second value with:

    ree2=re.match(r'^[0-9].[0-9]*$', ree1[1])

    print("ree2 {}".format(ree2))

however this is returning None .

Any thoughts? Thanks

CodePudding user response:

Unclear if you wanted the whole string, or individual parts, but you can do both without ^ or $

import re

regex = r'((?P<major>\d )\.(?P<minor>\d )\.(?P<patch>\d ))'
s = '9.0.1 and earlier\n\n\n\n\n'

matches = re.search(regex, s)
print(matches.group(0))
for v in ['major', 'minor', 'patch']:
    print(v, matches.group(v))

Output

9.0.1
major 9
minor 0
patch 1

CodePudding user response:

i used this one and it worked for me since dollar sign means the end of pattern and your pattern does not end with a number between 0-9 then it gives you none

regexPattern = "[0-9]\.[0-9]\.[0-9]*"
  • Related