Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/phonenumbers/re_util.py: 100%

6 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:30 +0000

1"""Additional regular expression utilities, to make it easier to sync up 

2with Java regular expression code. 

3 

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 

25 

26 

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): 

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 

40 

41 

42if __name__ == '__main__': # pragma no cover 

43 import doctest 

44 doctest.testmod()