I've been developing a program that has a function that takes a string like so: ((1.(0.2)2).0) and returns True if it is a regex. Here's mmy code so far:
def is_regex(s):
#Takes a string 's' and produces True if it is a valid regular expression
#But False otherwise
ex = '( + 3* + | + 6* + )'
leaves = ['1', '2', '0', '1*', '2*', '0*']
internal_nodes = ['|', '.']
stripped_string = s.strip('()')
if len(s.strip('()')) == 1:
if '0' in s.strip('()') or '1' in s.strip('()') or '2' in s.strip('()') or 'e' in s.strip('()'):
return True
elif len(s.strip('()')) == 0:
return True
elif stripped_string in leaves[3:6]:
return True
elif len(stripped_string) == 3:
if stripped_string[0] in leaves and stripped_string[2] in leaves:
if stripped_string[1] in internal_nodes:
return True
elif '.' in s and len(stripped_string) > 3:
dot_position = s.rfind('.')
if s.rfind('.') > s.rfind(')', 0, len(s)-1): #if '.' is only surrounded by one set of () then it is the root of the tree
is_regex(s[dot_position +1])
The idea here is I want to find the root of the tree, and check if it's two children are valid regexes, if they are I move on a recurse until I'm at the leaf, if it passes all the tests until it hits the leaf then the entire regex is True.
For the last line I have there is_regex(s[dot_position +1]) I'm not getting anything returned, even though I know s[dot_position +1] returns 0 so there should be no problem. In this line I'm checking the right child of . which is 0.
Edit: Another question: I need to test whether or not the left child is true as well. How would I do this? Wouldn't I need to pass both to is_regex? Or should I check if both right and left are true then proceed? This is confusing
returnstatement to the last line, why does that make it work?