Answers for "3.4.2. Two Special Characters¶"

0

3.4.2. Two Special Characters¶

import re
def checkvalid(string1):
    if re.search(r'[\d.]\s+[\d.]', string1): return False # two numbers separated by spaces
    string1 = string1.replace(' ', '')
    return (bool(re.search(r'\d', string1))               # must contain numbers
        and not re.search(r'\.\d*\.', string1)            # a number can't contain two dots
        and not re.search(r'[$^]{2}|#[$^]|[$^]#[$^#]', string1) # obviously
        and not re.search(r'^[$^]|[$^]$', string1)        # special at the beginning or end
        and not re.search(r'^##|##$', string1)            # two hashes at the beginning or end
    )
Posted by: Guest on March-22-2022

Browse Popular Code Answers by Language