home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / regsub.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  5.8 KB  |  175 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Regexp-based split and replace using the obsolete regex module.
  5.  
  6. This module is only for backward compatibility.  These operations
  7. are now provided by the new regular expression module, "re".
  8.  
  9. sub(pat, repl, str):        replace first occurrence of pattern in string
  10. gsub(pat, repl, str):       replace all occurrences of pattern in string
  11. split(str, pat, maxsplit):  split string using pattern as delimiter
  12. splitx(str, pat, maxsplit): split string using pattern as delimiter plus
  13.                             return delimiters
  14. '''
  15. import warnings
  16. warnings.warn('the regsub module is deprecated; please use re.sub()', DeprecationWarning)
  17. warnings.filterwarnings('ignore', '', DeprecationWarning, __name__)
  18. import regex
  19. __all__ = [
  20.     'sub',
  21.     'gsub',
  22.     'split',
  23.     'splitx',
  24.     'capwords']
  25.  
  26. def sub(pat, repl, str):
  27.     prog = compile(pat)
  28.     if prog.search(str) >= 0:
  29.         regs = prog.regs
  30.         (a, b) = regs[0]
  31.         str = str[:a] + expand(repl, regs, str) + str[b:]
  32.     
  33.     return str
  34.  
  35.  
  36. def gsub(pat, repl, str):
  37.     prog = compile(pat)
  38.     new = ''
  39.     start = 0
  40.     first = 1
  41.     while prog.search(str, start) >= 0:
  42.         regs = prog.regs
  43.         (a, b) = regs[0]
  44.         if b == b:
  45.             pass
  46.         elif b == start and not first:
  47.             if start >= len(str) or prog.search(str, start + 1) < 0:
  48.                 break
  49.             
  50.             regs = prog.regs
  51.             (a, b) = regs[0]
  52.         
  53.         new = new + str[start:a] + expand(repl, regs, str)
  54.         start = b
  55.         first = 0
  56.     new = new + str[start:]
  57.     return new
  58.  
  59.  
  60. def split(str, pat, maxsplit = 0):
  61.     return intsplit(str, pat, maxsplit, 0)
  62.  
  63.  
  64. def splitx(str, pat, maxsplit = 0):
  65.     return intsplit(str, pat, maxsplit, 1)
  66.  
  67.  
  68. def intsplit(str, pat, maxsplit, retain):
  69.     prog = compile(pat)
  70.     res = []
  71.     start = next = 0
  72.     splitcount = 0
  73.     while prog.search(str, next) >= 0:
  74.         regs = prog.regs
  75.         (a, b) = regs[0]
  76.         if a == b:
  77.             next = next + 1
  78.             if next >= len(str):
  79.                 break
  80.             
  81.         else:
  82.             res.append(str[start:a])
  83.             if retain:
  84.                 res.append(str[a:b])
  85.             
  86.             start = next = b
  87.             splitcount = splitcount + 1
  88.             if maxsplit and splitcount >= maxsplit:
  89.                 break
  90.             
  91.     res.append(str[start:])
  92.     return res
  93.  
  94.  
  95. def capwords(str, pat = '[^a-zA-Z0-9_]+'):
  96.     words = splitx(str, pat)
  97.     for i in range(0, len(words), 2):
  98.         words[i] = words[i].capitalize()
  99.     
  100.     return ''.join(words)
  101.  
  102. cache = { }
  103.  
  104. def compile(pat):
  105.     if type(pat) != type(''):
  106.         return pat
  107.     
  108.     key = (pat, regex.get_syntax())
  109.     if cache.has_key(key):
  110.         prog = cache[key]
  111.     else:
  112.         prog = cache[key] = regex.compile(pat)
  113.     return prog
  114.  
  115.  
  116. def clear_cache():
  117.     global cache
  118.     cache = { }
  119.  
  120.  
  121. def expand(repl, regs, str):
  122.     if '\\' not in repl:
  123.         return repl
  124.     
  125.     new = ''
  126.     i = 0
  127.     ord0 = ord('0')
  128.     while i < len(repl):
  129.         c = repl[i]
  130.         i = i + 1
  131.         if c != '\\' or i >= len(repl):
  132.             new = new + c
  133.         else:
  134.             c = repl[i]
  135.             i = i + 1
  136.             if c <= c:
  137.                 pass
  138.             elif c <= '9':
  139.                 (a, b) = regs[ord(c) - ord0]
  140.                 new = new + str[a:b]
  141.             elif c == '\\':
  142.                 new = new + c
  143.             else:
  144.                 new = new + '\\' + c
  145.     return new
  146.  
  147.  
  148. def test():
  149.     import sys
  150.     if sys.argv[1:]:
  151.         delpat = sys.argv[1]
  152.     else:
  153.         delpat = '[ \t\n]+'
  154.     while 1:
  155.         if sys.stdin.isatty():
  156.             sys.stderr.write('--> ')
  157.         
  158.         line = sys.stdin.readline()
  159.         if not line:
  160.             break
  161.         
  162.         if line[-1] == '\n':
  163.             line = line[:-1]
  164.         
  165.         fields = split(line, delpat)
  166.         if len(fields) != 3:
  167.             print 'Sorry, not three fields'
  168.             print 'split:', `fields`
  169.             continue
  170.         
  171.         (pat, repl, str) = split(line, delpat)
  172.         print 'sub :', `sub(pat, repl, str)`
  173.         print 'gsub:', `gsub(pat, repl, str)`
  174.  
  175.