本文整理汇总了Python中pypy.rlib.rsre.rsre_re.match函数的典型用法代码示例。如果您正苦于以下问题:Python match函数的具体用法?Python match怎么用?Python match使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了match函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bug_527371
def test_bug_527371(self):
# bug described in patches 527371/672491
assert re.match(r'(a)?a','a').lastindex == None
assert re.match(r'(a)(b)?b','ab').lastindex == 1
assert re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup == 'a'
assert re.match("(?P<a>a(b))", "ab").lastgroup == 'a'
assert re.match("((a))", "a").lastindex == 1
示例2: test_bug_448951
def test_bug_448951(self):
# bug 448951 (similar to 429357, but with single char match)
# (Also test greedy matches.)
for op in '','?','*':
assert re.match(r'((.%s):)?z'%op, 'z').groups() == (
(None, None))
assert re.match(r'((.%s):)?z'%op, 'a:z').groups() == (
('a:', 'a'))
示例3: test_getlower
def test_getlower(self):
import _sre
assert _sre.getlower(ord('A'), 0) == ord('a')
assert _sre.getlower(ord('A'), re.LOCALE) == ord('a')
assert _sre.getlower(ord('A'), re.UNICODE) == ord('a')
assert re.match("abc", "ABC", re.I).group(0) == "ABC"
assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
示例4: test_re_escape
def test_re_escape(self):
p=""
for i in range(0, 256):
p = p + chr(i)
assert re.match(re.escape(chr(i)), chr(i)) is not None
assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)
pat=re.compile(re.escape(p))
assert pat.match(p) is not None
assert pat.match(p).span() == (0,256)
示例5: test_bug_418626
def test_bug_418626(self):
# bugs 418626 at al. -- Testing Greg Chapman's addition of op code
# SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
# pattern '*?' on a long string.
assert re.match('.*?c', 10000*'ab'+'cd').end(0) == 20001
assert re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0) == (
20003)
assert re.match('.*?cd', 20000*'abc'+'de').end(0) == 60001
# non-simple '*?' still used to hit the recursion limit, before the
# non-recursive scheme was implemented.
assert re.search('(a|b)*?c', 10000*'ab'+'cd').end(0) == 20001
示例6: test_search_star_plus
def test_search_star_plus(self):
assert re.search('x*', 'axx').span(0) == (0, 0)
assert re.search('x*', 'axx').span() == (0, 0)
assert re.search('x+', 'axx').span(0) == (1, 3)
assert re.search('x+', 'axx').span() == (1, 3)
assert re.search('x', 'aaa') == None
assert re.match('a*', 'xxx').span(0) == (0, 0)
assert re.match('a*', 'xxx').span() == (0, 0)
assert re.match('x*', 'xxxa').span(0) == (0, 3)
assert re.match('x*', 'xxxa').span() == (0, 3)
assert re.match('a+', 'xxx') == None
示例7: test_ignore_case
def test_ignore_case(self):
assert re.match(r"(a\s[^a])", "a b", re.I).group(1) == "a b"
assert re.match(r"(a\s[^a]*)", "a bb", re.I).group(1) == "a bb"
assert re.match(r"(a\s[abc])", "a b", re.I).group(1) == "a b"
assert re.match(r"(a\s[abc]*)", "a bb", re.I).group(1) == "a bb"
assert re.match(r"((a)\s\2)", "a a", re.I).group(1) == "a a"
assert re.match(r"((a)\s\2*)", "a aa", re.I).group(1) == "a aa"
assert re.match(r"((a)\s(abc|a))", "a a", re.I).group(1) == "a a"
assert re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1) == "a aa"
示例8: test_re_match
def test_re_match(self):
assert re.match('a', 'a').groups() == ()
assert re.match('(a)', 'a').groups() == ('a',)
assert re.match(r'(a)', 'a').group(0) == 'a'
assert re.match(r'(a)', 'a').group(1) == 'a'
assert re.match(r'(a)', 'a').group(1, 1) == ('a', 'a')
pat = re.compile('((a)|(b))(c)?')
assert pat.match('a').groups() == ('a', 'a', None, None)
assert pat.match('b').groups() == ('b', None, 'b', None)
assert pat.match('ac').groups() == ('a', 'a', None, 'c')
assert pat.match('bc').groups() == ('b', None, 'b', 'c')
assert pat.match('bc').groups("") == ('b', "", 'b', 'c')
# A single group
m = re.match('(a)', 'a')
assert m.group(0) == 'a'
assert m.group(0) == 'a'
assert m.group(1) == 'a'
assert m.group(1, 1) == ('a', 'a')
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
assert pat.match('a').group(1, 2, 3) == ('a', None, None)
assert pat.match('b').group('a1', 'b2', 'c3') == (
(None, 'b', None))
assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
示例9: test_sre_character_class_literals
def test_sre_character_class_literals(self):
for i in [0, 8, 16, 32, 64, 127, 128, 255]:
assert re.match(r"[\%03o]" % i, chr(i)) != None
assert re.match(r"[\%03o0]" % i, chr(i)) != None
assert re.match(r"[\%03o8]" % i, chr(i)) != None
assert re.match(r"[\x%02x]" % i, chr(i)) != None
assert re.match(r"[\x%02x0]" % i, chr(i)) != None
assert re.match(r"[\x%02xz]" % i, chr(i)) != None
raises(re.error, re.match, "[\911]", "")
示例10: test_sre_character_literals
def test_sre_character_literals(self):
for i in [0, 8, 16, 32, 64, 127, 128, 255]:
assert re.match(r"\%03o" % i, chr(i)) != None
assert re.match(r"\%03o0" % i, chr(i)+"0") != None
assert re.match(r"\%03o8" % i, chr(i)+"8") != None
assert re.match(r"\x%02x" % i, chr(i)) != None
assert re.match(r"\x%02x0" % i, chr(i)+"0") != None
assert re.match(r"\x%02xz" % i, chr(i)+"z") != None
raises(re.error, re.match, "\911", "")
示例11: test_bug_725106
def test_bug_725106(self):
# capturing groups in alternatives in repeats
assert re.match('^((a)|b)*', 'abc').groups() == (
('b', 'a'))
assert re.match('^(([ab])|c)*', 'abc').groups() == (
('c', 'b'))
assert re.match('^((d)|[ab])*', 'abc').groups() == (
('b', None))
assert re.match('^((a)c|[ab])*', 'abc').groups() == (
('b', None))
assert re.match('^((a)|b)*?c', 'abc').groups() == (
('b', 'a'))
assert re.match('^(([ab])|c)*?d', 'abcd').groups() == (
('c', 'b'))
assert re.match('^((d)|[ab])*?c', 'abc').groups() == (
('b', None))
assert re.match('^((a)c|[ab])*?c', 'abc').groups() == (
('b', None))
示例12: test_re_groupref
def test_re_groupref(self):
assert re.match(r'^(\|)?([^()]+)\1$', '|a|').groups() == (
('|', 'a'))
assert re.match(r'^(\|)?([^()]+)\1?$', 'a').groups() == (
(None, 'a'))
assert re.match(r'^(\|)?([^()]+)\1$', 'a|') == None
assert re.match(r'^(\|)?([^()]+)\1$', '|a') == None
assert re.match(r'^(?:(a)|c)(\1)$', 'aa').groups() == (
('a', 'a'))
assert re.match(r'^(?:(a)|c)(\1)?$', 'c').groups() == (
(None, None))
示例13: test_re_groupref_exists
def test_re_groupref_exists(self):
assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == (
('(', 'a'))
assert re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups() == (
(None, 'a'))
assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None
assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None
assert re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups() == (
('a', 'b'))
assert re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups() == (
(None, 'd'))
assert re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups() == (
(None, 'd'))
assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == (
('a', ''))
# Tests for bug #1177831: exercise groups other than the first group
p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
assert p.match('abc').groups() == (
('a', 'b', 'c'))
assert p.match('ad').groups() == (
('a', None, 'd'))
assert p.match('abd') == None
assert p.match('ac') == None
示例14: test_stack_overflow
def test_stack_overflow(self):
# nasty cases that used to overflow the straightforward recursive
# implementation of repeated groups.
assert re.match('(x)*', 50000*'x').group(1) == 'x'
assert re.match('(x)*y', 50000*'x'+'y').group(1) == 'x'
assert re.match('(x)*?y', 50000*'x'+'y').group(1) == 'x'
示例15: test_groupdict
def test_groupdict(self):
assert re.match('(?P<first>first) (?P<second>second)',
'first second').groupdict() == (
{'first':'first', 'second':'second'})