user2690972
user2690972

Reputation: 141

Verify if string is valid regex?

EDIT: I cannot use python's 're'

This is for an assignment so I don't want the answer in code, rather just some tips in the right direction.

I am trying to code a function that returns True if string s is a valid regex expression.

Examples of valid regex:
       '0'
       '1'
       '2'
       'e'
       '0*' etc..
       '(0*.1*)'
       '((0.1).2)' 

        '(0|1)'

The general rule is that if r1 and r2 are valid regex's '(' + r1 + 'valid symbol' (either | or . ) + 'r2' + ')' is valid.

The way I want it to work is to examine the string by parts recursively but I don't know how to actually do it properly. I've spent far too long on this and wondering if I could get some help.

I'll post my code, but it really doesn't work how I want at all. Mostly just my thoughts written down. The print statements are just there so I know what's going on during a call.

def check_regex(s):
    values = ['0', '1', '2', 'e'] 
    symbols = ['|', '*', '.']

    return (all([i in values if len(i) == 1 else 
                     i[0] in values and i[1] == '*' if len(i) == 2 else 
                     #check_regex([i[0], i[2]]) if len(i) == 3 else 
                     #check_regex([i[0:2], i[3:]]) if len(i) == 5 else
                    False for i in s])) 

def is_regex(s):
    bool_list = []
    symbols = ['|', '.']  

    if s.startswith('('):
        for i in range(len(s)):
            if s[i] in symbols: 
                r1 = s[1:i]
                r2 = s[i + 1:s.find(')', i)]
                #if r1.startswith('('):
                    #r1 = r1[1:]
                    #if r1.endswith(')'):
                        #r1 = r1[:-1]

                print('r1: {} r2: {}'.format(r1, r2))
                bool_list += [check_regex([r1,r2])]
                #bool_list += [is_regex(r1)]
                #bool_list += [is_regex(r2)]
    else: 
        bool_list += [check_regex([s])]

    print(bool_list)
    return all(bool_list)

Upvotes: 0

Views: 2180

Answers (1)

Adam Smith
Adam Smith

Reputation: 54213

Personally I would do this in a MUCH easier fashion....

def check_regex(s):
    try: re.compile(s)
    except Exception: return False
    return True

Basically, this is EAFP. Try to compile it as a regex. If that throws an error, then return False. Otherwise, return True.

Upvotes: 1

Related Questions