From 0e4a1ab7e160817f832b6f80e63324d4c7b6d212 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 6 May 2015 00:08:04 +0300 Subject: [PATCH] re-pcre: Implement findall() method. --- re-pcre/re.py | 19 +++++++++++++++++++ re-pcre/test_re.py | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/re-pcre/re.py b/re-pcre/re.py index 76365cdc..c4e4d43c 100644 --- a/re-pcre/re.py +++ b/re-pcre/re.py @@ -122,6 +122,21 @@ class PCREPattern: res.append(s) return res + def findall(self, s): + res = [] + while True: + m = self.search(s) + if not m: + return res + if m.num == 1: + res.append(m.group(0)) + elif m.num == 2: + res.append(m.group(1)) + else: + res.append(m.groups()) + beg, end = m.span(0) + s = s[end:] + def compile(pattern, flags=0): errptr = bytes(4) @@ -150,6 +165,10 @@ def split(pattern, s, maxsplit=0, flags=0): r = compile(pattern, flags) return r.split(s, maxsplit) +def findall(pattern, s, flags=0): + r = compile(pattern, flags) + return r.findall(s) + def escape(s): res = "" diff --git a/re-pcre/test_re.py b/re-pcre/test_re.py index fd31fc67..1dc18a5b 100644 --- a/re-pcre/test_re.py +++ b/re-pcre/test_re.py @@ -33,3 +33,12 @@ assert re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE) == ['0', '3', '9'] assert re.split('(\W+)', '...words, words...') == ['', '...', 'words', ', ', 'words', '...', ''] assert re.sub(r"[ :/?&]", "_", "http://foo.ua/bar/?a=1&b=baz/") == "http___foo.ua_bar__a=1_b=baz_" + +text = "He was carefully disguised but captured quickly by police." +assert re.findall(r"\w+ly", text) == ['carefully', 'quickly'] + +text = "He was carefully disguised but captured quickly by police." +assert re.findall(r"(\w+)(ly)", text) == [('careful', 'ly'), ('quick', 'ly')] + +text = "He was carefully disguised but captured quickly by police." +assert re.findall(r"(\w+)ly", text) == ['careful', 'quick']