def test_qualified_re_split(self): assert re.split(":", ":a:b::c", 2) == ['', 'a', 'b::c'] assert re.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d'] assert re.split("(:)", ":a:b::c", 2) == ( ['', ':', 'a', ':', 'b::c']) assert re.split("(:*)", ":a:b::c", 2) == ( ['', ':', 'a', ':', 'b::c'])
def rsre_example(): # scan through string looking for the first location where the regular # expression pattern produces a match print re.search('x*', 'axx').span(0) # if zero or more characters at the beginning of string match the regular # expression pattern print re.match('a+', 'xxx') # split string by the occurrences of pattern print re.split(":", ":a:b::c") # return all non-overlapping matches of pattern in string, as a list of # strings print re.findall(":+", "a:b::c:::d")
def f(i): if i: s = "aaaaaa" else: s = "caaaaa" print rsre_re.match("(a|b)aa", s) print rsre_re.match("a{4}", s) print rsre_re.search("(a|b)aa", s) print rsre_re.search("a{4}", s) for x in rsre_re.findall("(a|b)a", s): print x for x in rsre_re.findall("a{2}", s): print x for x in rsre_re.finditer("(a|b)a", s): print x for x in rsre_re.finditer("a{2}", s): print x for x in rsre_re.split("(a|b)a", s): print x for x in rsre_re.split("a{2}", s): print x return 0
def rsre_example(): # simulate scanf, %s - %d errors, %d warnings print re.search(r"(\S+) - (\d+) errors, (\d+) warnings", "/usr/sbin/sendmail - 0 errors, 4 warnings").groups() # making a phone book text = """Ross McFluff: 834.345.1254 155 Elm Street Ronald Heathmore: 892.345.3428 436 Finley Avenue Frank Burger: 925.541.7625 662 South Dogwood Way Heather Albrecht: 548.326.4584 919 Park Place""" entries = re.split("\n+", text) print[re.split(":? ", entry, 4) for entry in entries] # finding all adverbs text = "He was carefully disguised but captured quickly by police." print re.findall(r"\w+ly", text)
def test_re_split(self): assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c'] assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c'] assert re.split("(:*)", ":a:b::c") == (['', ':', 'a', ':', 'b', '::', 'c']) assert re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c'] assert re.split("(:)*", ":a:b::c") == (['', ':', 'a', ':', 'b', ':', 'c']) assert re.split("([b:]+)", ":a:b::c") == (['', ':', 'a', ':b::', 'c']) assert re.split("(b)|(:+)", ":a:b::c") == ([ '', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c' ]) assert re.split("(?:b)|(?::+)", ":a:b::c") == (['', 'a', '', '', 'c'])
def test_re_split(self): assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c'] assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c'] assert re.split("(:*)", ":a:b::c") == ( ['', ':', 'a', ':', 'b', '::', 'c']) assert re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c'] assert re.split("(:)*", ":a:b::c") == ( ['', ':', 'a', ':', 'b', ':', 'c']) assert re.split("([b:]+)", ":a:b::c") == ( ['', ':', 'a', ':b::', 'c']) assert re.split("(b)|(:+)", ":a:b::c") == ( ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']) assert re.split("(?:b)|(?::+)", ":a:b::c") == ( ['', 'a', '', '', 'c'])
def test_qualified_re_split(self): assert re.split(":", ":a:b::c", 2) == ['', 'a', 'b::c'] assert re.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d'] assert re.split("(:)", ":a:b::c", 2) == (['', ':', 'a', ':', 'b::c']) assert re.split("(:*)", ":a:b::c", 2) == (['', ':', 'a', ':', 'b::c'])