diff options
author | Georg Brandl <georg@python.org> | 2013-10-13 07:18:45 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2013-10-13 07:18:45 (GMT) |
commit | 4300019e1a6b20f6e2e780a36d96d795c9e71a6f (patch) | |
tree | d198489f28f652705acb40993adb686cd1ce917f /Lib | |
parent | 57841ddb5cc347884ff91c619007c43bf6a18a6b (diff) | |
download | cpython-4300019e1a6b20f6e2e780a36d96d795c9e71a6f.zip cpython-4300019e1a6b20f6e2e780a36d96d795c9e71a6f.tar.gz cpython-4300019e1a6b20f6e2e780a36d96d795c9e71a6f.tar.bz2 |
Add re.fullmatch() function and regex.fullmatch() method, which anchor the
pattern at both ends of the string to match.
Patch by Matthew Barnett.
Closes #16203.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/re.py | 28 | ||||
-rw-r--r-- | Lib/test/test_re.py | 24 |
2 files changed, 41 insertions, 11 deletions
@@ -85,16 +85,17 @@ resulting RE will match the second character. \\ Matches a literal backslash. This module exports the following functions: - match Match a regular expression pattern to the beginning of a string. - search Search a string for the presence of a pattern. - sub Substitute occurrences of a pattern found in a string. - subn Same as sub, but also return the number of substitutions made. - split Split a string by the occurrences of a pattern. - findall Find all occurrences of a pattern in a string. - finditer Return an iterator yielding a match object for each match. - compile Compile a pattern into a RegexObject. - purge Clear the regular expression cache. - escape Backslash all non-alphanumerics in a string. + match Match a regular expression pattern to the beginning of a string. + fullmatch Match a regular expression pattern to all of a string. + search Search a string for the presence of a pattern. + sub Substitute occurrences of a pattern found in a string. + subn Same as sub, but also return the number of substitutions made. + split Split a string by the occurrences of a pattern. + findall Find all occurrences of a pattern in a string. + finditer Return an iterator yielding a match object for each match. + compile Compile a pattern into a RegexObject. + purge Clear the regular expression cache. + escape Backslash all non-alphanumerics in a string. Some of the functions in this module takes flags as optional parameters: A ASCII For string patterns, make \w, \W, \b, \B, \d, \D @@ -123,7 +124,7 @@ import sre_compile import sre_parse # public symbols -__all__ = [ "match", "search", "sub", "subn", "split", "findall", +__all__ = [ "match", "fullmatch", "search", "sub", "subn", "split", "findall", "compile", "purge", "template", "escape", "A", "I", "L", "M", "S", "X", "U", "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", "UNICODE", "error" ] @@ -154,6 +155,11 @@ def match(pattern, string, flags=0): a match object, or None if no match was found.""" return _compile(pattern, flags).match(string) +def fullmatch(pattern, string, flags=0): + """Try to apply the pattern to all of the string, returning + a match object, or None if no match was found.""" + return _compile(pattern, flags).fullmatch(string) + def search(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.""" diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 2104437..ea57d1f 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1061,6 +1061,30 @@ class ReTests(unittest.TestCase): self.assertEqual(m.group(1), "") self.assertEqual(m.group(2), "y") + def test_fullmatch(self): + # Issue 16203: Proposal: add re.fullmatch() method. + self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1)) + self.assertEqual(re.fullmatch(r"a|ab", "ab").span(), (0, 2)) + self.assertEqual(re.fullmatch(r".*?$", "abc").span(), (0, 3)) + self.assertEqual(re.fullmatch(r".*?", "abc").span(), (0, 3)) + self.assertEqual(re.fullmatch(r"a.*?b", "ab").span(), (0, 2)) + self.assertEqual(re.fullmatch(r"a.*?b", "abb").span(), (0, 3)) + self.assertEqual(re.fullmatch(r"a.*?b", "axxb").span(), (0, 4)) + self.assertEqual(re.fullmatch(r"abc$", "abc\n"), None) + self.assertEqual(re.fullmatch(r"abc\Z", "abc\n"), None) + self.assertEqual(re.fullmatch(r"(?m)abc$", "abc\n"), None) + self.assertEqual(re.fullmatch(r"ab(?=c)cd", "abcd").span(), (0, 4)) + self.assertEqual(re.fullmatch(r"ab(?<=b)cd", "abcd").span(), (0, 4)) + self.assertEqual(re.fullmatch(r"(?=a|ab)ab", "ab").span(), (0, 2)) + + self.assertEqual( + re.compile(r"bc").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) + self.assertEqual( + re.compile(r".*?$").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) + self.assertEqual( + re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) + + def run_re_tests(): from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR if verbose: |