Advertisement
Guest User

Untitled

a guest
Jan 28th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. import sys
  2. import re
  3.  
  4. try: #python 2
  5.     _strtype = basestring
  6. except NameError: #python 3
  7.     _strtype = str
  8.  
  9. # Check equivalence of two regexes
  10. # Each arg is either a str or a pair (str,flags)
  11. def regex_eq(arg0,arg1):
  12.     return re_debug(arg0) == re_debug(arg1)
  13.  
  14. #compile and catch the output
  15. # Note: This clears the re module cache!
  16. def re_debug(args):
  17.    
  18.     #break the args up into patterns and flags
  19.     if isinstance(args,str): #if it's a string
  20.         pattern = args
  21.         flags = re.DEBUG
  22.     else:
  23.         pattern = args[0]
  24.         flags = re.DEBUG | args[1] #else add to flag
  25.    
  26.     #save the real stdout, then compile with debug
  27.     stdout = sys.stdout
  28.     try:
  29.         #to catch output of re.compile
  30.         logger = writer()
  31.         sys.stdout = logger
  32.        
  33.         #and compile
  34.         re.purge() #a cache'd result
  35.         re.compile(pattern,flags)
  36.     finally:
  37.         #uninstall the catcher
  38.         sys.stdout = stdout
  39.     return logger.log
  40.  
  41. class writer:
  42.     #sys.stdout needs a write method
  43.     def write(self,data):
  44.         self.log += data
  45.     def __init__(self):
  46.         self.log = '';
  47.  
  48.  
  49. ### Example ###
  50. re0 = "foo[bar]baz"
  51. re1 = ("""foo   # foo!
  52.          [bar] # b or a or r!
  53.          baz   # baz!"""
  54.        , re.VERBOSE)
  55.  
  56. regex_eq(re0,re1) #true
  57. regex_eq('123','123*') #false
  58. regex_eq('11*','1+') #true, returns false
  59. regex_eq('(1)','1') #true, returns false
  60. regex_eq('1?1*','1*') #true, returns false
  61.  
  62. '''
  63. (Python 2 printing)
  64.  
  65. >>> print re_debug('11*')
  66. literal 49
  67. max_repeat 0 2147483647
  68.  literal 49
  69.  
  70. >>> print re_debug('1+')
  71. max_repeat 1 2147483647
  72.  literal 49
  73.  
  74. >>> print re_debug('(1)')
  75. subpattern 1
  76.  literal 49
  77. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement