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

Public Member Functions

def __init__
 
def search
 
def match
 
def sub
 
def subn
 
def split
 
def findall
 
def __getinitargs__
 
def __getstate__
 
def __setstate__
 

Data Fields

 code
 
 flags
 
 pattern
 
 groupindex
 

Detailed Description

Holds a compiled regular expression pattern.

Methods:
match    Match the pattern to the beginning of a string.
search   Search a string for the presence of the pattern.
sub      Substitute occurrences of the pattern found in a string.
subn     Same as sub, but also return the number of substitutions made.
split    Split a string by the occurrences of the pattern.
findall  Find all occurrences of the pattern in a string.

Definition at line 253 of file pre.py.

Constructor & Destructor Documentation

def __init__ (   self,
  pattern,
  flags,
  code,
  groupindex 
)

Definition at line 266 of file pre.py.

267  def __init__(self, pattern, flags, code, groupindex):
268  self.code = code
269  self.flags = flags
270  self.pattern = pattern
271  self.groupindex = groupindex

Member Function Documentation

def __getinitargs__ (   self)

Definition at line 489 of file pre.py.

490  def __getinitargs__(self):
491  return (None,None,None,None) # any 4 elements, to work around
492  # problems with the
493  # pickle/cPickle modules not yet
# ignoring the __init__ function
def __getstate__ (   self)

Definition at line 494 of file pre.py.

References Compile.flags, RegexObject.flags, RegexObject.groupindex, and RegexObject.pattern.

495  def __getstate__(self):
return self.pattern, self.flags, self.groupindex
def __setstate__ (   self,
  statetuple 
)

Definition at line 496 of file pre.py.

References RegexObject.code, Compile.flags, RegexObject.flags, RegexObject.groupindex, and RegexObject.pattern.

497  def __setstate__(self, statetuple):
498  self.pattern = statetuple[0]
499  self.flags = statetuple[1]
500  self.groupindex = statetuple[2]
501  self.code = apply(pcre_compile, statetuple)
def findall (   self,
  source 
)
findall(source) -> list

Return a list of all non-overlapping matches of the compiled
pattern in string. If one or more groups are present in the
pattern, return a list of groups; this will be a list of
tuples if the pattern has more than one group. Empty matches
are included in the result.

Definition at line 452 of file pre.py.

References reconvert.append, RegexObject.match(), sre_parse.max, and log_faction_ships.tuple.

453  def findall(self, source):
454  """findall(source) -> list
455 
456  Return a list of all non-overlapping matches of the compiled
457  pattern in string. If one or more groups are present in the
458  pattern, return a list of groups; this will be a list of
459  tuples if the pattern has more than one group. Empty matches
460  are included in the result.
461 
462  """
463  pos = 0
464  end = len(source)
465  results = []
466  match = self.code.match
467  append = results.append
468  while pos <= end:
469  regs = match(source, pos, end, 0)
470  if not regs:
471  break
472  i, j = regs[0]
473  rest = regs[1:]
474  if not rest:
475  gr = source[i:j]
476  elif len(rest) == 1:
477  a, b = rest[0]
478  gr = source[a:b]
479  else:
480  gr = []
481  for (a, b) in rest:
482  gr.append(source[a:b])
483  gr = tuple(gr)
484  append(gr)
485  pos = max(j, pos+1)
486  return results
def match (   self,
  string,
  pos = 0,
  endpos = None 
)
match(string[, pos][, endpos]) -> MatchObject or None

If zero or more characters at the beginning of string match
this regular expression, return a corresponding MatchObject
instance. Return None if the string does not match the
pattern; note that this is different from a zero-length match.

Note: If you want to locate a match anywhere in string, use
search() instead.

The optional second parameter pos gives an index in the string
where the search is to start; it defaults to 0.  This is not
completely equivalent to slicing the string; the '' pattern
character matches at the real beginning of the string and at
positions just after a newline, but not necessarily at the
index where the search is to start.

The optional parameter endpos limits how far the string will
be searched; it will be as if the string is endpos characters
long, so only the characters from pos to endpos will be
searched for a match.

Definition at line 297 of file pre.py.

References RegexObject._num_regs.

298  def match(self, string, pos=0, endpos=None):
299  """match(string[, pos][, endpos]) -> MatchObject or None
300 
301  If zero or more characters at the beginning of string match
302  this regular expression, return a corresponding MatchObject
303  instance. Return None if the string does not match the
304  pattern; note that this is different from a zero-length match.
305 
306  Note: If you want to locate a match anywhere in string, use
307  search() instead.
308 
309  The optional second parameter pos gives an index in the string
310  where the search is to start; it defaults to 0. This is not
311  completely equivalent to slicing the string; the '' pattern
312  character matches at the real beginning of the string and at
313  positions just after a newline, but not necessarily at the
314  index where the search is to start.
315 
316  The optional parameter endpos limits how far the string will
317  be searched; it will be as if the string is endpos characters
318  long, so only the characters from pos to endpos will be
319  searched for a match.
320 
321  """
322  if endpos is None or endpos>len(string):
323  endpos=len(string)
324  if endpos<pos: endpos=pos
325  regs = self.code.match(string, pos, endpos, ANCHORED)
326  if regs is None:
327  return None
328  self._num_regs=len(regs)
329  return MatchObject(self,
330  string,
331  pos, endpos,
332  regs)
def search (   self,
  string,
  pos = 0,
  endpos = None 
)
search(string[, pos][, endpos]) -> MatchObject or None

Scan through string looking for a location where this regular
expression produces a match, and return a corresponding
MatchObject instance. Return None if no position in the string
matches the pattern; note that this is different from finding
a zero-length match at some point in the string. The optional
pos and endpos parameters have the same meaning as for the
match() method.

Definition at line 272 of file pre.py.

References RegexObject._num_regs.

273  def search(self, string, pos=0, endpos=None):
274  """search(string[, pos][, endpos]) -> MatchObject or None
275 
276  Scan through string looking for a location where this regular
277  expression produces a match, and return a corresponding
278  MatchObject instance. Return None if no position in the string
279  matches the pattern; note that this is different from finding
280  a zero-length match at some point in the string. The optional
281  pos and endpos parameters have the same meaning as for the
282  match() method.
283 
284  """
285  if endpos is None or endpos>len(string):
286  endpos=len(string)
287  if endpos<pos: endpos=pos
288  regs = self.code.match(string, pos, endpos, 0)
289  if regs is None:
290  return None
291  self._num_regs=len(regs)
292 
293  return MatchObject(self,
294  string,
295  pos, endpos,
296  regs)
def split (   self,
  source,
  maxsplit = 0 
)
split(source[, maxsplit=0]) -> list of strings

Split string by the occurrences of the compiled pattern. If
capturing parentheses are used in the pattern, then the text
of all groups in the pattern are also returned as part of the
resulting list. If maxsplit is nonzero, at most maxsplit
splits occur, and the remainder of the string is returned as
the final element of the list.

Definition at line 405 of file pre.py.

References reconvert.append, and RegexObject.match().

406  def split(self, source, maxsplit=0):
407  """split(source[, maxsplit=0]) -> list of strings
408 
409  Split string by the occurrences of the compiled pattern. If
410  capturing parentheses are used in the pattern, then the text
411  of all groups in the pattern are also returned as part of the
412  resulting list. If maxsplit is nonzero, at most maxsplit
413  splits occur, and the remainder of the string is returned as
414  the final element of the list.
415 
416  """
417  if maxsplit < 0:
418  raise error, "negative split count"
419  if maxsplit == 0:
420  maxsplit = sys.maxint
421  n = 0
422  pos = 0
423  lastmatch = 0
424  results = []
425  end = len(source)
426  match = self.code.match
427  append = results.append
428  while n < maxsplit:
429  regs = match(source, pos, end, 0)
430  if not regs:
431  break
432  i, j = regs[0]
433  if i == j:
434  # Empty match
435  if pos >= end:
436  break
437  pos = pos+1
438  continue
439  append(source[lastmatch:i])
440  rest = regs[1:]
441  if rest:
442  for a, b in rest:
443  if a == -1 or b == -1:
444  group = None
445  else:
446  group = source[a:b]
447  append(group)
448  pos = lastmatch = j
449  n = n + 1
450  append(source[lastmatch:])
451  return results
def sub (   self,
  repl,
  string,
  count = 0 
)
sub(repl, string[, count=0]) -> string

Return the string obtained by replacing the leftmost
non-overlapping occurrences of the compiled pattern in string
by the replacement repl. If the pattern isn't found, string is
returned unchanged.

Identical to the sub() function, using the compiled pattern.

Definition at line 333 of file pre.py.

References RegexObject.subn().

334  def sub(self, repl, string, count=0):
335  """sub(repl, string[, count=0]) -> string
336 
337  Return the string obtained by replacing the leftmost
338  non-overlapping occurrences of the compiled pattern in string
339  by the replacement repl. If the pattern isn't found, string is
340  returned unchanged.
341 
342  Identical to the sub() function, using the compiled pattern.
343 
344  """
345  return self.subn(repl, string, count)[0]
def subn (   self,
  repl,
  source,
  count = 0 
)
subn(repl, string[, count=0]) -> tuple

Perform the same operation as sub(), but return a tuple
(new_string, number_of_subs_made).

Definition at line 346 of file pre.py.

References RegexObject._num_regs, reconvert.append, regsub.expand(), dospath.join(), and RegexObject.match().

347  def subn(self, repl, source, count=0):
348  """subn(repl, string[, count=0]) -> tuple
349 
350  Perform the same operation as sub(), but return a tuple
351  (new_string, number_of_subs_made).
352 
353  """
354  if count < 0:
355  raise error, "negative substitution count"
356  if count == 0:
357  count = sys.maxint
358  n = 0 # Number of matches
359  pos = 0 # Where to start searching
360  lastmatch = -1 # End of last match
361  results = [] # Substrings making up the result
362  end = len(source)
363 
364  if type(repl) is type(''):
365  # See if repl contains group references
366  try:
367  repl = pcre_expand(_Dummy, repl)
368  except error:
369  m = MatchObject(self, source, 0, end, [])
370  repl = lambda m, repl=repl, expand=pcre_expand: expand(m, repl)
371  else:
372  m = None
373  else:
374  m = MatchObject(self, source, 0, end, [])
375 
376  match = self.code.match
377  append = results.append
378  while n < count and pos <= end:
379  regs = match(source, pos, end, 0)
380  if not regs:
381  break
382  self._num_regs = len(regs)
383  i, j = regs[0]
384  if i == j == lastmatch:
385  # Empty match adjacent to previous match
386  pos = pos + 1
387  append(source[lastmatch:pos])
388  continue
389  if pos < i:
390  append(source[pos:i])
391  if m:
392  m.pos = pos
393  m.regs = regs
394  append(repl(m))
395  else:
396  append(repl)
397  pos = lastmatch = j
398  if i == j:
399  # Last match was empty; don't try here again
400  pos = pos + 1
401  append(source[lastmatch:pos])
402  n = n + 1
403  append(source[pos:])
404  return (''.join(results), n)

Field Documentation

code

Definition at line 267 of file pre.py.

flags

Definition at line 268 of file pre.py.

groupindex

Definition at line 270 of file pre.py.

pattern

Definition at line 269 of file pre.py.


The documentation for this class was generated from the following file: