Coverage for presidio_analyzer / predefined_recognizers / generic / email_recognizer.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-29 09:03 +0000

1from typing import List, Optional 

2 

3import tldextract 

4 

5from presidio_analyzer import Pattern, PatternRecognizer 

6 

7 

8class EmailRecognizer(PatternRecognizer): 

9 """ 

10 Recognize email addresses using regex. 

11 

12 :param patterns: List of patterns to be used by this recognizer 

13 :param context: List of context words to increase confidence in detection 

14 :param supported_language: Language this recognizer supports 

15 :param supported_entity: The entity this recognizer can detect 

16 """ 

17 

18 PATTERNS = [ 

19 Pattern( 

20 "Email (Medium)", 

21 r"\b((([!#$%&'*+\-/=?^_`{|}~\w])|([!#$%&'*+\-/=?^_`{|}~\w][!#$%&'*+\-/=?^_`{|}~\.\w]{0,}[!#$%&'*+\-/=?^_`{|}~\w]))[@]\w+([-.]\w+)*\.\w+([-.]\w+)*)\b", 

22 0.5, 

23 ), 

24 ] 

25 

26 CONTEXT = ["email"] 

27 

28 def __init__( 

29 self, 

30 patterns: Optional[List[Pattern]] = None, 

31 context: Optional[List[str]] = None, 

32 supported_language: str = "en", 

33 supported_entity: str = "EMAIL_ADDRESS", 

34 name: Optional[str] = None, 

35 ): 

36 patterns = patterns if patterns else self.PATTERNS 

37 context = context if context else self.CONTEXT 

38 super().__init__( 

39 supported_entity=supported_entity, 

40 patterns=patterns, 

41 context=context, 

42 supported_language=supported_language, 

43 name=name, 

44 ) 

45 

46 def validate_result(self, pattern_text: str): # noqa: D102 

47 result = tldextract.extract(pattern_text) 

48 return result.fqdn != ""