TransWikia.com

Python regex for numbers between 0-63

Stack Overflow Asked by Perplexityy on November 18, 2021

I have written this regex to match the numbers in the aforementioned range but it’s not working properly, for instance it matches "00" and "64", and I don’t get why. My logic is "numbers between 0 or 9" or "numbers between 10-59" or numbers between "60-63".

import re


def six_bit_number(n):
    if re.match("([0-9]|[1-5][0-9]|6[0-3])", str(n)):
        return True
    return False

3 Answers

In addition to Ry's answer, simply changing to

re.fullmatch

worked.

Answered by Perplexityy on November 18, 2021

Try the below regex:

^([0-9]|[1-5][0-9]|(6[0123]))$ [Include Global and Multiline flag]

Understanding of the regex:

[0-9] --> Matches single-digit numbers from 0 to 9

[1-5][0-9] --> Matches double digit numbers from 10 to 59

(6[0123]) --> Matches 60,61,62 and 63

Answered by mishsx on November 18, 2021

re.match looks for a match at the start of the input, but it doesn’t mind if there’s also text after the match. You can add an anchor to make sure it also checks for the end of the string:

r"([0-9]|[1-5][0-9]|6[0-3])Z"

Answered by Ry- on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP