All Samples(295) | Call(273) | Derive(0) | Import(22)
Return a 2-tuple containing (new_string, number). new_string is the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in the source string by the replacement repl. number is the number of substitutions that were made. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.
def subn(pattern, repl, string, count=0, flags=0):
"""Return a 2-tuple containing (new_string, number).
new_string is the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in the source
string by the replacement repl. number is the number of
substitutions that were made. repl can be either a string or a
callable; if a string, backslash escapes in it are processed.
If it is a callable, it's passed the match object and must
return a replacement string to be used."""
return _compile(pattern, flags).subn(repl, string, count)
exp = re.compile("%s\s+" % target, re.MULTILINE)
# Remove the first instance of the screenname.
messout = re.subn(exp,"",omessage,1)[0]
# Ok, we'll try to forward it.
self.do_SEND_IM(target,messout)
src/p/y/py-toc-HEAD/samples/ProxyBot.py py-toc(Download)
exp = re.compile("%s\s+" % target, re.MULTILINE)
# Remove the first instance of the screenname.
messout = re.subn(exp,"",omessage,1)[0]
# Ok, we'll try to forward it.
self.do_SEND_IM(target,messout)
src/g/u/gunicorn-0.11.1/examples/websocket/websocket.py gunicorn(Download)
def _get_key_value(self, key_value):
if not key_value:
return
key_number = int(re.sub("\\D", "", key_value))
spaces = re.subn(" ", "", key_value)[1]
if key_number % spaces != 0:
return
src/g/u/gunicorn-0.11.1/examples/websocket/gevent_websocket.py gunicorn(Download)
def _get_key_value(self, key_value):
if not key_value:
return
key_number = int(re.sub("\\D", "", key_value))
spaces = re.subn(" ", "", key_value)[1]
if key_number % spaces != 0:
return
src/w/e/wecowi-HEAD/trunk/tools/pywecowi/table2wiki.py wecowi(Download)
# bring every <tag> into one single line.
num = 1
while num != 0:
newTable, num = re.subn("([^\r\n]{1})(<[tT]{1}[dDhHrR]{1})",
r"\1\r\n\2", newTable)
##################
# fail save. sometimes people forget </th>
# <th> without attributes, without closing </th>
newTable, n = re.subn("(?i)[\r\n]+<th>(?P<header>[\w\W]*?)[\r\n]+",
r"\r\n! \g<header>\r\n", newTable)
if n>0:
warning_messages.append(u'WARNING: found <th> without </th>. (%d occurences)\n' % n)
warnings += n
# <th> with attributes, without closing </th>
newTable, n = re.subn("(?i)[\r\n]+<th(?P<attr> [^>]*?)>(?P<header>[\w\W]*?)[\r\n]+",
# WARNING: this sub might eat cells of bad HTML, but most likely it
# will correct errors
# TODO: some more docu please
newTable, n = re.subn("(?i)[\r\n]+<td>(?P<cell>[^\r\n]*?)<td>",
r"\r\n| \g<cell>\r\n", newTable)
if n>0:
warning_messages.append(u'<td> used where </td> was expected. (%d occurences)\n' % n)
# warnings += n
# what is this for?
newTable, n = re.subn("[\r\n]+<(td|TD)([^>]+?)>([^\r\n]*?)<\/(td|TD)>",
r"\r\n|\2 | \3\r\n", newTable)
if n>0:
warning_messages.append(u'WARNING: (sorry, bot code unreadable (1). I don\'t know why this warning is given.) (%d occurences)\n' % n)
# fail save. sometimes people forget </td>
# <td> without arguments, with missing </td>
newTable, n = re.subn("(?i)<td>(?P<cell>[^<]*?)[\r\n]+",
warning_messages.append(u'NOTE: Found <td> without </td>. This shouldn\'t cause problems.\n')
# <td> with attributes, with missing </td>
newTable, n = re.subn("(?i)[\r\n]*<td(?P<attr> [^>]*?)>(?P<cell>[\w\W]*?)[\r\n]+",
r"\r\n|\g<attr> | \g<cell>\r\n", newTable)
if n > 0:
warning_messages.append(u'NOTE: Found <td> without </td>. This shouldn\'t cause problems.\n')
if config.deIndentTables:
num = 1
while num != 0:
newTable, num = re.subn("(\{\|[\w\W]*?)\n[ \t]+([\w\W]*?\|\})",
r"\1\r\n\2", newTable)
##################
# group 2 is the value of the attribute we want to fix here.
# We recognize it by searching for a string of non-whitespace characters
# - [^\s]+? - which is not embraced by quotation marks - [^"]
newTable, num = re.subn(r'([\r\n]+(?:\|-|\{\|)[^\r\n\|]+) *= *([^"\s>]+)',
r'\1="\2"', newTable, 1)
num = 1
while num != 0:
# The same for header and cell tags ( ! or | ), but for these tags the
# attribute part is finished by a | character. We don't want to change
# cell contents which accidentially contain an equal sign.
# Group 1 and 2 are anologously to the previous regular expression,
# group 3 are the remaining attribute key - value pairs.
newTable, num = re.subn(r'([\r\n]+(?:!|\|)[^\r\n\|]+) *= *([^"\s>]+)([^\|\r\n]*)\|',
# merge two short <td>s
num = 1
while num != 0:
newTable, num = re.subn("[\r\n]+(\|[^\|\-\}]{1}[^\n\r]{0,35})" +
"[\r\n]+(\|[^\|\-\}]{1}[^\r\n]{0,35})[\r\n]+",
r"\r\n\1 |\2\r\n", newTable)
####
# kill additional spaces within arguments
num = 1
while num != 0:
newTable, num = re.subn("[\r\n]+(\||\!)([^|\r\n]*?)[ \t]{2,}([^\r\n]+?)",
r"\r\n\1\2 \3", newTable)
##################
while num != 0:
# TODO: how does this work? docu please.
# why are only äöüß used, but not other special characters?
newTable, num = re.subn("(\r\n[A-Z]{1}[^\n\r]{200,}?[a-zäöüß]\.)\ ([A-ZÄÖÜ]{1}[^\n\r]{200,})",
r"\1\r\n\2", newTable)
return newTable, warnings, warning_messages
src/w/e/wecowi-HEAD/tools/pywecowi/table2wiki.py wecowi(Download)
# bring every <tag> into one single line.
num = 1
while num != 0:
newTable, num = re.subn("([^\r\n]{1})(<[tT]{1}[dDhHrR]{1})",
r"\1\r\n\2", newTable)
##################
# fail save. sometimes people forget </th>
# <th> without attributes, without closing </th>
newTable, n = re.subn("(?i)[\r\n]+<th>(?P<header>[\w\W]*?)[\r\n]+",
r"\r\n! \g<header>\r\n", newTable)
if n>0:
warning_messages.append(u'WARNING: found <th> without </th>. (%d occurences)\n' % n)
warnings += n
# <th> with attributes, without closing </th>
newTable, n = re.subn("(?i)[\r\n]+<th(?P<attr> [^>]*?)>(?P<header>[\w\W]*?)[\r\n]+",
# WARNING: this sub might eat cells of bad HTML, but most likely it
# will correct errors
# TODO: some more docu please
newTable, n = re.subn("(?i)[\r\n]+<td>(?P<cell>[^\r\n]*?)<td>",
r"\r\n| \g<cell>\r\n", newTable)
if n>0:
warning_messages.append(u'<td> used where </td> was expected. (%d occurences)\n' % n)
# warnings += n
# what is this for?
newTable, n = re.subn("[\r\n]+<(td|TD)([^>]+?)>([^\r\n]*?)<\/(td|TD)>",
r"\r\n|\2 | \3\r\n", newTable)
if n>0:
warning_messages.append(u'WARNING: (sorry, bot code unreadable (1). I don\'t know why this warning is given.) (%d occurences)\n' % n)
# fail save. sometimes people forget </td>
# <td> without arguments, with missing </td>
newTable, n = re.subn("(?i)<td>(?P<cell>[^<]*?)[\r\n]+",
warning_messages.append(u'NOTE: Found <td> without </td>. This shouldn\'t cause problems.\n')
# <td> with attributes, with missing </td>
newTable, n = re.subn("(?i)[\r\n]*<td(?P<attr> [^>]*?)>(?P<cell>[\w\W]*?)[\r\n]+",
r"\r\n|\g<attr> | \g<cell>\r\n", newTable)
if n > 0:
warning_messages.append(u'NOTE: Found <td> without </td>. This shouldn\'t cause problems.\n')
if config.deIndentTables:
num = 1
while num != 0:
newTable, num = re.subn("(\{\|[\w\W]*?)\n[ \t]+([\w\W]*?\|\})",
r"\1\r\n\2", newTable)
##################
# group 2 is the value of the attribute we want to fix here.
# We recognize it by searching for a string of non-whitespace characters
# - [^\s]+? - which is not embraced by quotation marks - [^"]
newTable, num = re.subn(r'([\r\n]+(?:\|-|\{\|)[^\r\n\|]+) *= *([^"\s>]+)',
r'\1="\2"', newTable, 1)
num = 1
while num != 0:
# The same for header and cell tags ( ! or | ), but for these tags the
# attribute part is finished by a | character. We don't want to change
# cell contents which accidentially contain an equal sign.
# Group 1 and 2 are anologously to the previous regular expression,
# group 3 are the remaining attribute key - value pairs.
newTable, num = re.subn(r'([\r\n]+(?:!|\|)[^\r\n\|]+) *= *([^"\s>]+)([^\|\r\n]*)\|',
# merge two short <td>s
num = 1
while num != 0:
newTable, num = re.subn("[\r\n]+(\|[^\|\-\}]{1}[^\n\r]{0,35})" +
"[\r\n]+(\|[^\|\-\}]{1}[^\r\n]{0,35})[\r\n]+",
r"\r\n\1 |\2\r\n", newTable)
####
# kill additional spaces within arguments
num = 1
while num != 0:
newTable, num = re.subn("[\r\n]+(\||\!)([^|\r\n]*?)[ \t]{2,}([^\r\n]+?)",
r"\r\n\1\2 \3", newTable)
##################
while num != 0:
# TODO: how does this work? docu please.
# why are only äöüß used, but not other special characters?
newTable, num = re.subn("(\r\n[A-Z]{1}[^\n\r]{200,}?[a-zäöüß]\.)\ ([A-ZÄÖÜ]{1}[^\n\r]{200,})",
r"\1\r\n\2", newTable)
return newTable, warnings, warning_messages
src/i/n/inflect-0.2.1/inflect.py inflect(Download)
''' from re import match, search, subn, IGNORECASE, VERBOSE from re import split as splitre from re import error as reerror from re import sub as resub
inflection = []
for section in sections:
(section, count) = subn(r"num\(\s*?(?:([^),]*)(?:,([^)]*))?)?\)", self.nummo, section)
if not count:
total = -1
while total:
(section, total) = subn(
r"(?x)\bplural \( ([^),]*) (, ([^)]*) )? \) ",
self.plmo, section)
(section, count) = subn(
r"(?x)\bplural_noun \( ([^),]*) (, ([^)]*) )? \) ",
self.plnounmo, section)
total += count
(section, count) = subn(
r"(?x)\bplural_verb \( ([^),]*) (, ([^)]*) )? \) ",
self.plverbmo, section)
total += count
(section, count) = subn(
r"(?x)\bplural_adj \( ([^),]*) (, ([^)]*) )? \) ",
self.pladjmo, section)
total += count
(section, count) = subn(
r"(?x)\bsingular_noun \( ([^),]*) (, ([^)]*) )? \) ",
self.sinounmo, section)
total += count
(section, count) = subn(
r"(?x)\ban? \( ([^),]*) (, ([^)]*) )? \) ",
self.amo, section)
total += count
(section, count) = subn(
r"(?x)\bno \( ([^),]*) (, ([^)]*) )? \) ",
self.nomo, section)
total += count
(section, count) = subn(
r"(?x)\bordinal \( ([^)]*) \) ",
self.ordinalmo, section)
total += count
(section, count) = subn(
r"(?x)\bnumwords \( ([^)]*) \) ",
self.numwordsmo, section)
total += count
(section, count) = subn(
r"(?x)\bpresent_participle \( ([^)]*) \) ",
(r"er$", r"er"),
(r"([^aeiou][aeiouy]([bdgmnprst]))$", "\g<1>\g<2>"),
):
(ans, num) = subn(pat, repl, plv)
if num:
return "%sing" % ans
return "%sing" % ans
float(num) > threshold):
spnum = num.split('.',1)
while (comma):
(spnum[0], n) = subn(r"(\d)(\d{3}(?:,|\Z))",r"\1,\2", spnum[0])
if n==0:
break
try:
src/p/y/py-mycms-HEAD/trunk/MyCMS/tools/__init__.py py-mycms(Download)
from MyCMS.tools import *
from os import getpid, stat
from time import time, ctime
from re import subn, compile, sub, search
DTIME = compile( '^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$' )
MOS = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
def spaceOut( text, debug=None ):
try:
if debug: print "try sub<BR>", str(text), "<P>"
return subn( '\n', " <BR>", subn( '\n{2}', " <P>", subn( '\n\r', '\n', str(text) )[0] )[0] )[0]
except Exception, ec:
if debug: print "No substitution<BR>", str(ec)
return text
def replace(oStr,cf,cr):
return subn(cf,cr,oStr)[0]
def replaceChar(oStr,cf,cr):
""" find the specified character in a string and replace it with another character """
nStr = ""
def subString(oStr,sf,sr):
""" find the specified string and replace with another string """
nStr = subn(sf,sr,str(oStr))
return nStr[0]
def makeList(toList):
src/p/y/py-mycms-HEAD/MyCMS/tools/__init__.py py-mycms(Download)
from MyCMS.tools import *
from os import getpid, stat
from time import time, ctime
from re import subn, compile, sub, search
DTIME = compile( '^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$' )
MOS = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
def spaceOut( text, debug=None ):
try:
if debug: print "try sub<BR>", str(text), "<P>"
return subn( '\n', " <BR>", subn( '\n{2}', " <P>", subn( '\n\r', '\n', str(text) )[0] )[0] )[0]
except Exception, ec:
if debug: print "No substitution<BR>", str(ec)
return text
def replace(oStr,cf,cr):
return subn(cf,cr,oStr)[0]
def replaceChar(oStr,cf,cr):
""" find the specified character in a string and replace it with another character """
nStr = ""
def subString(oStr,sf,sr):
""" find the specified string and replace with another string """
nStr = subn(sf,sr,str(oStr))
return nStr[0]
def makeList(toList):
src/p/y/pypes-HEAD/ui/trunk/pypesvds/lib/extras/elementfilter.py pypes(Download)
def sub(self, pattern, repl, count=0, deep=False):
changes = 0
for elem in self.filtered:
if self.attribute is None:
newval, i = re.subn(pattern, repl, elem.text, count)
elem.text = newval
changes += i
if deep:
for child in elem.getiterator():
newval, i = re.subn(pattern, repl, child.text, count)
child.text = newval
changes += i
newval, i = re.subn(pattern, repl, child.tail, count)
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Next