Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import re
- try: #python 2
- _strtype = basestring
- except NameError: #python 3
- _strtype = str
- # Check equivalence of two regexes
- # Each arg is either a str or a pair (str,flags)
- def regex_eq(arg0,arg1):
- return re_debug(arg0) == re_debug(arg1)
- #compile and catch the output
- # Note: This clears the re module cache!
- def re_debug(args):
- #break the args up into patterns and flags
- if isinstance(args,str): #if it's a string
- pattern = args
- flags = re.DEBUG
- else:
- pattern = args[0]
- flags = re.DEBUG | args[1] #else add to flag
- #save the real stdout, then compile with debug
- stdout = sys.stdout
- try:
- #to catch output of re.compile
- logger = writer()
- sys.stdout = logger
- #and compile
- re.purge() #a cache'd result
- re.compile(pattern,flags)
- finally:
- #uninstall the catcher
- sys.stdout = stdout
- return logger.log
- class writer:
- #sys.stdout needs a write method
- def write(self,data):
- self.log += data
- def __init__(self):
- self.log = '';
- ### Example ###
- re0 = "foo[bar]baz"
- re1 = ("""foo # foo!
- [bar] # b or a or r!
- baz # baz!"""
- , re.VERBOSE)
- regex_eq(re0,re1) #true
- regex_eq('123','123*') #false
- regex_eq('11*','1+') #true, returns false
- regex_eq('(1)','1') #true, returns false
- regex_eq('1?1*','1*') #true, returns false
- '''
- (Python 2 printing)
- >>> print re_debug('11*')
- literal 49
- max_repeat 0 2147483647
- literal 49
- >>> print re_debug('1+')
- max_repeat 1 2147483647
- literal 49
- >>> print re_debug('(1)')
- subpattern 1
- literal 49
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement