Home > Enterprise >  Validate number with multiple points
Validate number with multiple points

Time:08-08

I want to remove a version number for a string that has multiple dots, for instance app-9.6.0 should return 9.6.0 and app-960 should return None. I tried with the code bellow but it returns numbers without the dots either.

import re
re.findall(r'[\.\d] ', 'app-960')

How can I implement a parser or regex for this case?

CodePudding user response:

Try this:

import re
str_for_search = 'app-9.6.0'
search = re.search(r'\w -(\d \.\d \.\d )', str_for_search)
if search:
    version = search.group(1)
else:
    version = None
print(version)

CodePudding user response:

If app- is always going to be a consistent string, it's probably going to be faster and easier to do this without a regex:

def version_number(full_version):
    version_num = full_version.replace("app-", "", 1)
    # If I'm interpreting your question right, you also
    # want to validate that the version contains at least one dot
    if "." in version_num:
        return version_num
    else:
        return None

If you need to do this with a regex for some reason, others have given examples that should work.

CodePudding user response:

If you want to match app- and digits with 1 or more dots in between, you can use a capture group.

Start the capture with matching digits and then repeat 1 or more times matching a hyphen and again 1 or more digits.

Example

import re

pattern = r'app-(\d (?:\.\d ) )'
s = 'app-1 app-9.6.0, app-1.0, app-1.1.0, app-1.10.0, app-1.1.1.0'

print (re.findall(pattern, s))

Output

['9.6.0', '1.0', '1.1.0', '1.10.0', '1.1.1.0']

A broader variant matching 1 non whitespace chars with \S before the hyphen:

pattern = r'\S -(\d (?:\.\d ) )'

CodePudding user response:

is it always 2 dots?

if so you can do this

import re
re.findall(r'\d \.\d \.\d ', 'app-960')
re.findall(r'\d \.\d \.\d ', 'app-9.6.0')

if no dots is ok then what you got already works

if you want atleast 1 dot you can do:

re.findall(r'\d\.[\.\d] ', 'app-9.6.0')

edit:

you can do this to avoid the multiple dots in a row problem:

import re
re.findall(r'\d(?:\.\d) ', 'app-9.6.0 app=9..9 app1.1.1.1.1.1 app1')
  • Related