Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/phonenumbers/re_util.py: 64%
10 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""Additional regular expression utilities, to make it easier to sync up
2with Java regular expression code.
4>>> import re
5>>> from .re_util import fullmatch
6>>> from .util import u
7>>> string = 'abcd'
8>>> r1 = re.compile('abcd')
9>>> r2 = re.compile('bc')
10>>> r3 = re.compile('abc')
11>>> fullmatch(r1, string) # doctest: +ELLIPSIS
12<...Match object...>
13>>> fullmatch(r2, string)
14>>> fullmatch(r3, string)
15>>> r = re.compile(r'\\d{8}|\\d{10,11}')
16>>> m = fullmatch(r, '1234567890')
17>>> m.end()
1810
19>>> r = re.compile(u(r'[+\uff0b\\d]'), re.UNICODE)
20>>> m = fullmatch(r, u('\uff10'))
21>>> m.end()
221
23"""
24import re
27def fullmatch(pattern, string, flags=0):
28 """Try to apply the pattern at the start of the string, returning a match
29 object if the whole string matches, or None if no match was found."""
30 # Build a version of the pattern with a non-capturing group around it.
31 # This is needed to get m.end() to correctly report the size of the
32 # matched expression (as per the final doctest above).
33 grouped_pattern = re.compile("^(?:%s)$" % pattern.pattern, pattern.flags)
34 m = grouped_pattern.match(string)
35 if m and m.end() < len(string): 35 ↛ 38line 35 didn't jump to line 38, because the condition on line 35 was never true
36 # Incomplete match (which should never happen because of the $ at the
37 # end of the regexp), treat as failure.
38 m = None # pragma no cover
39 return m
42if __name__ == '__main__': # pragma no cover 42 ↛ 43line 42 didn't jump to line 43, because the condition on line 42 was never true
43 import doctest
44 doctest.testmod()