简体   繁体   中英

how to match a USN using regular expression in Python?

Given an USN: 1722AB3401 and range: 3401 to 3470.

if 1722AB3433 is given as an input it should display valid USN, if not (for instance: 1722AB3499) it should say Invalid USN.

How to solve this using python ?

I tried below approach (using Python 3.6.x):

import re

pattern = r"1722AB34[0-7][0-9]"

if re.search(pattern, "1722AB3471"):
    print("Valid USN")
else:
    print("Invalid USN")

But, if I try with input 1722AB3471 it would give me a wrong answer as the range is from *3401 to *3470

Note: USN is University Serial Number

Your expectation is wrong, since your regular expression clearly allows 3400..3479.

I don't favour the attempt to get the validation completely using the regular expression, even if it would work with a complicated one like.

pattern = r"1722AB34(([0-6][0-9])|70)"

I would try to extract the number following the characters and compare this numerically.

Regexes for mixed number ranges tend to be quite complicated. In your case, you would need the following for the range 3401–3470:

34(0[1-9]|[1-6][0-9]|70)

It only gets more complicated if the ranges get longer and more mixed inside of decimal places.

A better way would be to simply extract that number part, making the validation outside of the regular expression:

usn = '1722AB3471'

m = re.match('1722AB(\d{4})', usn)
if m and 3401 <= int(m.group(1)) <= 3470:
    print('Valid USN')
else:
    print('Invalid USN')

I would favor simply testing the last four digits of your USN. Add an additional term to your if statement:

import re

pattern = r"1722AB34[0-7][0-9]"

usn = "1722AB3470"

if re.search(pattern, usn) and int(usn[-4:]) in range(3401, 3472):
    print("Valid USN")
else:
    print("Invalid USN")

[0-7][0-9] will match from 00 to 79. You need to use:

pattern = r"1722AB34(0[1-9]|[1-6][0-9]|70)"

This will match from 01 to 09 or from 10 to 69 or 70

But getting the four last digit, convert then to integers and compare would be a better way.

Building on the suggestions given in the other answers, the best way to find if the USN is valid, is to check the range of the last four digits of the USN.

usn = '1722AB3469' # Given USN 
last_four_digits = int(usn[-4:]) # Slice the string to get only the last four digits, then convert it to an integer
unchanged_pattern = usn[:6] # Find the pattern that doesn't change across all USNs
if (last_four_digits >= 3401 and last_four_digits <= 3470) and unchanged_pattern == "1722AB": # Check the range of the extracted number, and also the unchanged pattern
    print('Valid USN')
else:
    print('Invalid USN')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM