本文整理汇总了Python中re.regex函数的典型用法代码示例。如果您正苦于以下问题:Python regex函数的具体用法?Python regex怎么用?Python regex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了regex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_html
def from_html(main_row, subtext_row):
''' Constructs Story from HN site markup elements.
Arguments are <tr> elements obtained with BeautifulSoup.
'''
link = main_row.find_all('td')[2].a
vote_td = main_row.find_all('td')[1]
subtext = subtext_row.find('td', {'class': 'subtext'})
comments_link = subtext.find('a', href=regex(r'item\?id\=.+'))
not_job = bool(comments_link)
story = {'title': link.text, 'url': link['href']}
if not_job:
points = cast(int, subtext.find('span', id=regex(r'score_\d+')
).text.split()[0], default=0)
comments_count = cast(int, comments_link.text.split()[0],
default=0)
story.update({
'author': subtext.find('a', href=regex(r'user\?id\=.+')).text,
'points': points,
'time': list(subtext.strings)[-2].replace('|', '').strip(),
'comments_count': comments_count,
'comments_url': comments_link['href'],
'upvote_url': vote_td.find('a', id=regex(r'up_\d+'))['href'],
})
url = story['comments_url']
else:
story['time'] = subtext.text
url = story['url']
story['id'] = int(url[url.find('=')+1:])
return Story(**story)
示例2: __init__
def __init__(self, name, pathPatterns, filePatterns, all_conditions = False):
self.name = name
self.allcond = all_conditions
self.fpat = []
self.ppat = []
for pat in filePatterns:
self.fpat.append(regex(pat))
for pat in pathPatterns:
self.ppat.append(regex(pat))
示例3: test0
def test0(self):
r'''
`_feed` and `_expected_expected` has been manually prepared reading
`_tree_template`. `expected` is programmatically prepared from
_tree_template.
'''
from ... import TestTree
from .....util import name_shift
from .....lib import SimplestFindBy, Str, StringIO
from re import compile as regex, escape
b = StringIO()
_tree_template.feed(b, 'p1.py\n', '', '0')
_tree_template.feed(b, 'p1+/\n', '', '1')
feed = b.getvalue()
expected = _tree_template.expected()
expected = tuple(expected)
abs_dir = join(self.tmp, 'a')
content = _content_feed
abs_dir = TestTree(abs_dir, feed, _content_feed)
replace, actual = name_shift(abs_dir)
for s in replace:
content = regex(r'\b' + escape(s[0]) + r'\b').sub(s[1], content)
cmp = []
find = SimplestFindBy(lambda prefix, infix, y: y.endswith(r'.py'))
for y in find(Str(abs_dir), ''):
with abs_dir.joinpath(y).open(r'rt') as istream:
cmp.append(istream.read() == content)
expected = [True] * len(cmp), expected
actual = cmp, actual
self.assertEqual(expected, actual)
示例4: main
def main():
"Detect all orphan files and entries"
g = Graph()
for manifest in [MANIFEST_PATH, MANIFEST_SYNTAX_PATH,
MANIFEST_TURTLE_LDPATCH_PATH,]:
g.load(manifest, format="turtle")
LOG.debug("loaded %s", manifest)
for manifest in [MANIFEST_TURTLE_PATH,]:
if exists(manifest):
g.load(manifest, format="turtle")
LOG.debug("loaded %s", manifest)
# looking for entries that are not part of an mf:entry list
for subj, _, _ in g.triples((None, MF.action, None)):
if not list(g.triples((None, RDF.first, subj))):
print subj
# looking for files that are not referenced
FILTER = regex(r'.*\.(ttl|nt|ldpatch)$')
for foldername in (TESTSUITE_PATH, TURTLE_PATH):
for filename in listdir(foldername):
if not FILTER.match(filename):
continue
if filename.startswith("manifest"):
continue
iri = p2i(join(foldername, filename))
if not list(g.triples((None, None, iri))):
print iri
# checking that all entries have the correct name
for subj, _, obj in g.triples((None, MF.name, None)):
if subj.rsplit("#",1)[1] != unicode(obj):
print "WRONG NAME for ", subj
示例5: test
def test(self):
from ....lib import Path, RegexFileEditor
from re import compile as regex
edit = RegexFileEditor(((regex(r'\ba\b'), 'x'), (regex(r'\b0\b'), '9')))
a, b = (Path(self.tmp).joinpath(x) for x in ('a', 'b'))
with a.open(r'wt') as ostream:
ostream.write(r'abc.a')
with b.open(r'wt') as ostream:
ostream.write(r'012.0')
for x in a, b:
edit(x)
with a.open(r'rt') as istream:
a = istream.read()
with b.open(r'rt') as istream:
b = istream.read()
expected = r'abc.x', r'012.9'
actual = a, b
self.assertEqual(expected, actual)
示例6: _ed
def _ed(substs, abs_dir):
from ..lib import RegexFileEditor, SimplestFindBy
from re import compile as regex, escape
olds = (y[0] for y in substs)
news = tuple(y[1] for y in substs)
oldpats = tuple(regex(r'\b' + escape(y) + r'\b') for y in olds)
find = SimplestFindBy(lambda prefix, infix, y: y.endswith(r'.py'))
for y in find(abs_dir, ''):
RegexFileEditor(zip(oldpats, news))(abs_dir.joinpath(y))
示例7: readConfig
def readConfig():
global notificationTimeout, errorTimeout, socketTimeout, \
checkInterval, sources
currentSource = None
file = open(getenv('HOME') + '/.config/catfriend', 'r')
re = regex("^\s*(?:([a-zA-Z_]+)(?:\s+(\S+))?\s*)?(?:#.*)?$")
checks = []
for source in sources:
checks.append(MailSource(source))
while True:
line = file.readline()
if not line: break
res = re.match(line)
if not res:
return line
res = res.groups()
if res[0] is None: continue
if res[0] == "notificationTimeout":
notificationTimeout = int(res[1])
elif res[0] == "errorTimeout":
errorTimeout = int(res[1])
elif res[0] == "socketTimeout":
socketTimeout = int(res[1])
elif res[0] == "checkInterval":
checkInterval = int(res[1])
elif res[0] == "host" or res[0] == "imap":
if currentSource:
sources.append(currentSource)
currentSource = MailSource(res[1])
elif currentSource is None:
return line
elif not res[1]:
if res[0] == "nossl":
currentSource.noSsl = True
else:
return line
elif res[0] == "id":
currentSource.id = res[1]
elif res[0] == "user":
currentSource.user = res[1]
elif res[0] == "password":
currentSource.password = res[1]
elif res[0] == "cert_file":
# ignored
currentSource.cert_file = res[1]
else:
return line
sources.append(currentSource)
示例8: get_filter
def get_filter():
"""a filter which takes out only filenames which probably contain media"""
extensions = ['avi', 'mpg', 'mpeg', 'mp4', 'mkv', 'ogv',
'flv', 'ogg', 'mov', 'mp3', 'ac3', 'rm', 'ram',
'wmv', '3gp', 'aac', 'asf', 'h263', 'webm',
'm4a', '3g2', 'mj2']
regexstring = r'\.('
for extension in extensions:
regexstring = regexstring + extension + '|'
regexstring = regexstring[:-1] + ')$'
return regex(regexstring).search
示例9: __prepare_fields
def __prepare_fields(self):
from re import match as regex
regstr = '(^`)([a-z]*)'
for i in range(len(self.select_dict['fields'])):
field, field_as = self.__ensure_field_as(self.select_dict['fields'][i])
if not regex(regstr, self.select_dict['fields'][i]):
self.select_dict['fields'][i] = '`a`.'+field+' AS '+field_as
示例10: is_non_proposal_filibuster
def is_non_proposal_filibuster(self, message):
""" Parses the message, determines if it is a filibustering non-proposal (D: or :D:)
:param message: Message to parse.
:type message: str
:returns: True if the message is 'D:', ':D:', or 'nick: D:', etc.
"""
npf_matcher = regex(r'<[^>]+> (?:(\S+)[:,] )?(:)?D:')
result = npf_matcher.match(message)
return False if result == None else True
示例11: is_ignored_message
def is_ignored_message(self, message):
""" Parses the message, determines if it should be ignored (does not reset votecount, is not a proposal)
:param message: Message to parse.
:type message: strings
:returns: True if the message should be ignored when legislating.
"""
jpq_matcher = regex(r'\*\*\* (Joins|Parts|Quits)')
result = jpq_matcher.match(message)
return False if result == None else True
示例12: __init__
def __init__(self, banned_words=BANNED_WORDS):
from tools import AudioEffect, HiddenTextEffect, ExplicitTextEffect, PhonemicEffect, \
VoiceEffect
self.effects = {cls: [] for cls in
(AudioEffect, HiddenTextEffect, ExplicitTextEffect, PhonemicEffect, VoiceEffect)}
self.connection_time = datetime.now()
self.last_attack = datetime.now() # any user has to wait some time before attacking, after entering the chan
self.last_message = datetime.now()
self.timestamps = list()
self.has_been_warned = False # User has been warned he shouldn't flood
self._banned_words = [regex(word) for word in banned_words]
self.is_shadowbanned = False # User has been shadowbanned
示例13: _retrieve_user_info
def _retrieve_user_info(self, page="/"):
""" Gets HN user info from given page.
A page is either an URL or BeautifulSoup object.
Returns True of False, depending on whether user info
could be found.
"""
if isinstance(page, basestring):
page = self._fetch_page(page)
top_table = page.find("table").find("table")
user_td = top_table.find_all("td")[-1]
user_span = user_td.find("span", {"class": "pagetop"})
user_link = user_span.find("a", href=regex(r"user\?id\=.+"))
if not user_link:
return False
name = user_link.text
points = regex(r"\((\d+)\)").search(user_span.text).group(1)
if not (name or points):
return False
self.user_name = name
self.user_points = points
return True
示例14: __init__
def __init__(self, e, v = None):
if isinstance(e, Exception) and v is None:
self._t = e.__class__
self._match = lambda s: s.startswith(str(e))
elif isinstance(e, type) and issubclass(e, Exception) and \
((v is None) or isinstance(v, str)):
self._t = e
if v is None:
self._match = lambda s: True
else:
v = regex("^(?:{0:s})$".format(v))
self._match = lambda s: v.match(s) is not None
else:
raise Exception("usage: with expected(Exception[, \"regex\"]): "
"or with expected(Exception(\"text\")):")
示例15: __init__
def __init__(self, line):
if regex(r"\n$", line):
line = line[:-1]
parts = line.split()
self.operator = ""
self.label = ""
self.location = -1
self.location_name = ""
if len(parts) == 1:
self.operator = parts[0]
elif len(parts) == 2:
if parts[0] in operators:
self.operator = parts[0]
if is_location(parts[1]):
self.location = int(parts[1])
else:
self.location_name = parts[1]
else:
self.operator = parts[1]
if self.operator == "DAT":
self.location_name = parts[0]
valid_location_names.append(self.location_name)
else:
self.label = parts[0]
elif len(parts) == 3:
self.operator = parts[1]
if self.operator == "DAT":
self.location_name = parts[0]
memory[self.location_name] = int(parts[2])
valid_location_names.append(self.location_name)
else:
self.label = parts[0]
if is_location(parts[2]):
self.location = int(parts[2])
else:
self.location_name = parts[2]