regsub.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """Regexp-based split and replace using the obsolete regex module.
  2. This module is only for backward compatibility. These operations
  3. are now provided by the new regular expression module, "re".
  4. sub(pat, repl, str): replace first occurrence of pattern in string
  5. gsub(pat, repl, str): replace all occurrences of pattern in string
  6. split(str, pat, maxsplit): split string using pattern as delimiter
  7. splitx(str, pat, maxsplit): split string using pattern as delimiter plus
  8. return delimiters
  9. """
  10. import warnings
  11. warnings.warn("the regsub module is deprecated; please use re.sub()",
  12. DeprecationWarning)
  13. # Ignore further deprecation warnings about this module
  14. warnings.filterwarnings("ignore", "", DeprecationWarning, __name__)
  15. import regex
  16. __all__ = ["sub","gsub","split","splitx","capwords"]
  17. # Replace first occurrence of pattern pat in string str by replacement
  18. # repl. If the pattern isn't found, the string is returned unchanged.
  19. # The replacement may contain references \digit to subpatterns and
  20. # escaped backslashes. The pattern may be a string or an already
  21. # compiled pattern.
  22. def sub(pat, repl, str):
  23. prog = compile(pat)
  24. if prog.search(str) >= 0:
  25. regs = prog.regs
  26. a, b = regs[0]
  27. str = str[:a] + expand(repl, regs, str) + str[b:]
  28. return str
  29. # Replace all (non-overlapping) occurrences of pattern pat in string
  30. # str by replacement repl. The same rules as for sub() apply.
  31. # Empty matches for the pattern are replaced only when not adjacent to
  32. # a previous match, so e.g. gsub('', '-', 'abc') returns '-a-b-c-'.
  33. def gsub(pat, repl, str):
  34. prog = compile(pat)
  35. new = ''
  36. start = 0
  37. first = 1
  38. while prog.search(str, start) >= 0:
  39. regs = prog.regs
  40. a, b = regs[0]
  41. if a == b == start and not first:
  42. if start >= len(str) or prog.search(str, start+1) < 0:
  43. break
  44. regs = prog.regs
  45. a, b = regs[0]
  46. new = new + str[start:a] + expand(repl, regs, str)
  47. start = b
  48. first = 0
  49. new = new + str[start:]
  50. return new
  51. # Split string str in fields separated by delimiters matching pattern
  52. # pat. Only non-empty matches for the pattern are considered, so e.g.
  53. # split('abc', '') returns ['abc'].
  54. # The optional 3rd argument sets the number of splits that are performed.
  55. def split(str, pat, maxsplit = 0):
  56. return intsplit(str, pat, maxsplit, 0)
  57. # Split string str in fields separated by delimiters matching pattern
  58. # pat. Only non-empty matches for the pattern are considered, so e.g.
  59. # split('abc', '') returns ['abc']. The delimiters are also included
  60. # in the list.
  61. # The optional 3rd argument sets the number of splits that are performed.
  62. def splitx(str, pat, maxsplit = 0):
  63. return intsplit(str, pat, maxsplit, 1)
  64. # Internal function used to implement split() and splitx().
  65. def intsplit(str, pat, maxsplit, retain):
  66. prog = compile(pat)
  67. res = []
  68. start = next = 0
  69. splitcount = 0
  70. while prog.search(str, next) >= 0:
  71. regs = prog.regs
  72. a, b = regs[0]
  73. if a == b:
  74. next = next + 1
  75. if next >= len(str):
  76. break
  77. else:
  78. res.append(str[start:a])
  79. if retain:
  80. res.append(str[a:b])
  81. start = next = b
  82. splitcount = splitcount + 1
  83. if (maxsplit and (splitcount >= maxsplit)):
  84. break
  85. res.append(str[start:])
  86. return res
  87. # Capitalize words split using a pattern
  88. def capwords(str, pat='[^a-zA-Z0-9_]+'):
  89. words = splitx(str, pat)
  90. for i in range(0, len(words), 2):
  91. words[i] = words[i].capitalize()
  92. return "".join(words)
  93. # Internal subroutines:
  94. # compile(pat): compile a pattern, caching already compiled patterns
  95. # expand(repl, regs, str): expand \digit escapes in replacement string
  96. # Manage a cache of compiled regular expressions.
  97. #
  98. # If the pattern is a string a compiled version of it is returned. If
  99. # the pattern has been used before we return an already compiled
  100. # version from the cache; otherwise we compile it now and save the
  101. # compiled version in the cache, along with the syntax it was compiled
  102. # with. Instead of a string, a compiled regular expression can also
  103. # be passed.
  104. cache = {}
  105. def compile(pat):
  106. if type(pat) != type(''):
  107. return pat # Assume it is a compiled regex
  108. key = (pat, regex.get_syntax())
  109. if key in cache:
  110. prog = cache[key] # Get it from the cache
  111. else:
  112. prog = cache[key] = regex.compile(pat)
  113. return prog
  114. def clear_cache():
  115. global cache
  116. cache = {}
  117. # Expand \digit in the replacement.
  118. # Each occurrence of \digit is replaced by the substring of str
  119. # indicated by regs[digit]. To include a literal \ in the
  120. # replacement, double it; other \ escapes are left unchanged (i.e.
  121. # the \ and the following character are both copied).
  122. def expand(repl, regs, str):
  123. if '\\' not in repl:
  124. return repl
  125. new = ''
  126. i = 0
  127. ord0 = ord('0')
  128. while i < len(repl):
  129. c = repl[i]; i = i+1
  130. if c != '\\' or i >= len(repl):
  131. new = new + c
  132. else:
  133. c = repl[i]; i = i+1
  134. if '0' <= c <= '9':
  135. a, b = regs[ord(c)-ord0]
  136. new = new + str[a:b]
  137. elif c == '\\':
  138. new = new + c
  139. else:
  140. new = new + '\\' + c
  141. return new
  142. # Test program, reads sequences "pat repl str" from stdin.
  143. # Optional argument specifies pattern used to split lines.
  144. def test():
  145. import sys
  146. if sys.argv[1:]:
  147. delpat = sys.argv[1]
  148. else:
  149. delpat = '[ \t\n]+'
  150. while 1:
  151. if sys.stdin.isatty(): sys.stderr.write('--> ')
  152. line = sys.stdin.readline()
  153. if not line: break
  154. if line[-1] == '\n': line = line[:-1]
  155. fields = split(line, delpat)
  156. if len(fields) != 3:
  157. print 'Sorry, not three fields'
  158. print 'split:', repr(fields)
  159. continue
  160. [pat, repl, str] = split(line, delpat)
  161. print 'sub :', repr(sub(pat, repl, str))
  162. print 'gsub:', repr(gsub(pat, repl, str))