Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
regsub Namespace Reference

Functions

def sub
 
def gsub
 
def split
 
def splitx
 
def intsplit
 
def capwords
 
def compile
 
def clear_cache
 
def expand
 
def test
 

Variables

list __all__ = ["sub","gsub","split","splitx","capwords"]
 
dictionary cache = {}
 

Detailed Description

Regexp-based split and replace using the obsolete regex module.

This module is only for backward compatibility.  These operations
are now provided by the new regular expression module, "re".

sub(pat, repl, str):        replace first occurrence of pattern in string
gsub(pat, repl, str):       replace all occurrences of pattern in string
split(str, pat, maxsplit):  split string using pattern as delimiter
splitx(str, pat, maxsplit): split string using pattern as delimiter plus
                        return delimiters

Function Documentation

def regsub.capwords (   str,
  pat = '[^a-zA-Z0-9_]+' 
)

Definition at line 110 of file regsub.py.

References string.capitalize(), dospath.join(), and splitx().

111 def capwords(str, pat='[^a-zA-Z0-9_]+'):
112  words = splitx(str, pat)
113  for i in range(0, len(words), 2):
114  words[i] = words[i].capitalize()
115  return "".join(words)
116 
117 
118 # Internal subroutines:
119 # compile(pat): compile a pattern, caching already compiled patterns
120 # expand(repl, regs, str): expand \digit escapes in replacement string
121 
122 
123 # Manage a cache of compiled regular expressions.
124 #
125 # If the pattern is a string a compiled version of it is returned. If
126 # the pattern has been used before we return an already compiled
127 # version from the cache; otherwise we compile it now and save the
128 # compiled version in the cache, along with the syntax it was compiled
129 # with. Instead of a string, a compiled regular expression can also
130 # be passed.
def regsub.clear_cache ( )

Definition at line 144 of file regsub.py.

145 def clear_cache():
146  global cache
147  cache = {}
148 
149 
150 # Expand \digit in the replacement.
151 # Each occurrence of \digit is replaced by the substring of str
152 # indicated by regs[digit]. To include a literal \ in the
153 # replacement, double it; other \ escapes are left unchanged (i.e.
154 # the \ and the following character are both copied).
def regsub.compile (   pat)

Definition at line 133 of file regsub.py.

134 def compile(pat):
135  if type(pat) != type(''):
136  return pat # Assume it is a compiled regex
137  key = (pat, regex.get_syntax())
138  if cache.has_key(key):
139  prog = cache[key] # Get it from the cache
140  else:
141  prog = cache[key] = regex.compile(pat)
142  return prog
143 
def regsub.expand (   repl,
  regs,
  str 
)

Definition at line 155 of file regsub.py.

156 def expand(repl, regs, str):
157  if '\\' not in repl:
158  return repl
159  new = ''
160  i = 0
161  ord0 = ord('0')
162  while i < len(repl):
163  c = repl[i]; i = i+1
164  if c != '\\' or i >= len(repl):
165  new = new + c
166  else:
167  c = repl[i]; i = i+1
168  if '0' <= c <= '9':
169  a, b = regs[ord(c)-ord0]
170  new = new + str[a:b]
171  elif c == '\\':
172  new = new + c
173  else:
174  new = new + '\\' + c
175  return new
176 
177 
178 # Test program, reads sequences "pat repl str" from stdin.
179 # Optional argument specifies pattern used to split lines.
def regsub.gsub (   pat,
  repl,
  str 
)

Definition at line 44 of file regsub.py.

References compile(), and expand().

44 
45 def gsub(pat, repl, str):
46  prog = compile(pat)
47  new = ''
48  start = 0
49  first = 1
50  while prog.search(str, start) >= 0:
51  regs = prog.regs
52  a, b = regs[0]
53  if a == b == start and not first:
54  if start >= len(str) or prog.search(str, start+1) < 0:
55  break
56  regs = prog.regs
57  a, b = regs[0]
58  new = new + str[start:a] + expand(repl, regs, str)
59  start = b
60  first = 0
61  new = new + str[start:]
62  return new
63 
64 
65 # Split string str in fields separated by delimiters matching pattern
66 # pat. Only non-empty matches for the pattern are considered, so e.g.
67 # split('abc', '') returns ['abc'].
68 # The optional 3rd argument sets the number of splits that are performed.
def regsub.intsplit (   str,
  pat,
  maxsplit,
  retain 
)

Definition at line 84 of file regsub.py.

References compile().

84 
85 def intsplit(str, pat, maxsplit, retain):
86  prog = compile(pat)
87  res = []
88  start = next = 0
89  splitcount = 0
90  while prog.search(str, next) >= 0:
91  regs = prog.regs
92  a, b = regs[0]
93  if a == b:
94  next = next + 1
95  if next >= len(str):
96  break
97  else:
98  res.append(str[start:a])
99  if retain:
100  res.append(str[a:b])
101  start = next = b
102  splitcount = splitcount + 1
103  if (maxsplit and (splitcount >= maxsplit)):
104  break
105  res.append(str[start:])
106  return res
107 
108 
109 # Capitalize words split using a pattern
def regsub.split (   str,
  pat,
  maxsplit = 0 
)

Definition at line 69 of file regsub.py.

References intsplit().

69 
70 def split(str, pat, maxsplit = 0):
71  return intsplit(str, pat, maxsplit, 0)
72 
73 # Split string str in fields separated by delimiters matching pattern
74 # pat. Only non-empty matches for the pattern are considered, so e.g.
75 # split('abc', '') returns ['abc']. The delimiters are also included
76 # in the list.
77 # The optional 3rd argument sets the number of splits that are performed.
78 
def regsub.splitx (   str,
  pat,
  maxsplit = 0 
)

Definition at line 79 of file regsub.py.

References intsplit().

79 
80 def splitx(str, pat, maxsplit = 0):
81  return intsplit(str, pat, maxsplit, 1)
82 
83 # Internal function used to implement split() and splitx().
def regsub.sub (   pat,
  repl,
  str 
)

Definition at line 30 of file regsub.py.

References compile(), and expand().

30 
31 def sub(pat, repl, str):
32  prog = compile(pat)
33  if prog.search(str) >= 0:
34  regs = prog.regs
35  a, b = regs[0]
36  str = str[:a] + expand(repl, regs, str) + str[b:]
37  return str
38 
39 
40 # Replace all (non-overlapping) occurrences of pattern pat in string
41 # str by replacement repl. The same rules as for sub() apply.
42 # Empty matches for the pattern are replaced only when not adjacent to
43 # a previous match, so e.g. gsub('', '-', 'abc') returns '-a-b-c-'.
def regsub.test ( )

Definition at line 180 of file regsub.py.

References gsub(), split(), and sub().

181 def test():
182  import sys
183  if sys.argv[1:]:
184  delpat = sys.argv[1]
185  else:
186  delpat = '[ \t\n]+'
187  while 1:
188  if sys.stdin.isatty(): sys.stderr.write('--> ')
189  line = sys.stdin.readline()
190  if not line: break
191  if line[-1] == '\n': line = line[:-1]
192  fields = split(line, delpat)
193  if len(fields) != 3:
194  print 'Sorry, not three fields'
195  print 'split:', `fields`
196  continue
197  [pat, repl, str] = split(line, delpat)
198  print 'sub :', `sub(pat, repl, str)`
199  print 'gsub:', `gsub(pat, repl, str)`

Variable Documentation

list __all__ = ["sub","gsub","split","splitx","capwords"]

Definition at line 22 of file regsub.py.

dictionary cache = {}

Definition at line 131 of file regsub.py.