0

I'm trying write the code to check the version format in regex. Is this the correct way to check the format

v0.0.0 -> \w\d[0-9].\d[0-9].\d[0-9]

Also, how do I compare using python and selenium web driver?

    def version(self):
    version = self.getElement("id","version")
    match = re.search(r'\w\d\.\d\.\d', version)
    if match:
        version = match.group()
        return True
    return False
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
user3174886
  • 191
  • 4
  • 17
  • 1
    What happens when you test it? Do you have reasons to think it is not correct? – Scott Hunter Dec 12 '16 at 16:22
  • 1
    `\d` and `[0-9]` mean the same thing, and `.` matches any character. I would check for version like `v\d+\.\d+\.\d+` https://regex101.com/r/HbLJoi/1 – Patrick Haugh Dec 12 '16 at 16:22
  • Consider adding capture groups to the expression. – Mad Physicist Dec 12 '16 at 16:24
  • Thanks @PatrickHaugh. I know I'm missing something. Thanks for the fix. – user3174886 Dec 12 '16 at 16:32
  • An Out Of Topic: Have you tried the parsers for the version? Just in case you need only to parse version and don't implement the relative regex http://stackoverflow.com/questions/11887762/compare-version-strings – gunzapper Dec 12 '16 at 16:36
  • No I didn't. this is what I did `match = re.search(r'v\d+\.\d+\.\d+', version_From_Page) if match: version = match.group() return True return False` – user3174886 Dec 13 '16 at 14:30

1 Answers1

3

Do you need to check each number individually? If so, you can use v(\d+)\.(\d+)\.(\d+) and look at each re group and compare.

. will match any character so you need to escape (\) it and \d already matches a single digit as it's equivalent to [0-9].

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
moogle
  • 110
  • 9