Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/render/html/env/regex.py: 0%

13 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-03 13:41 +0000

1import re 

2import typing as t 

3 

4from viur.core.render.html.utils import jinjaGlobalFunction 

5from ..default import Render 

6 

7 

8@jinjaGlobalFunction 

9def regexMatch(render: Render, pattern: str, string: str, flags: int = 0) -> re.Match: 

10 """ 

11 Jinja2 global: Match a string for regular expression pattern. 

12 This function internally runs re.match(). 

13 

14 :param render: The html-renderer instance. 

15 :param pattern: Regular expression pattern to be matched. 

16 :param string: String where to be searched in. 

17 :param flags: Flags to be passed to re.search(). 

18 

19 :return: A matching object on success, else None. 

20 """ 

21 return re.match(pattern, string, flags) 

22 

23 

24@jinjaGlobalFunction 

25def regexReplace(render: Render, string: str, pattern: str, replace: str) -> str: 

26 """ 

27 Jinja2 global: Replace string by regular expression pattern. 

28 

29 :param render: The html-renderer instance. 

30 :param string: String to be replaced. 

31 :param pattern: Regular expression pattern to be matched. 

32 :param replace: Replacement string to be inserted for every matching pattern. 

33 

34 :return: The string with the replaced matches. 

35 """ 

36 return re.sub(pattern, replace, string) 

37 

38 

39@jinjaGlobalFunction 

40def regexSearch(render: Render, string: str, pattern: str, flags=0) -> t.Optional[re.Match]: 

41 """ 

42 Jinja2 global: Search a string for regular expression pattern. 

43 This function internally runs re.search(). 

44 

45 :param render: The html-renderer instance. 

46 :param string: String where to be searched in. 

47 :param pattern: Regular expression pattern to be matched. 

48 :param flags: Flags to be passed to re.search(). 

49 

50 :return: A matching object on success, else None. 

51 """ 

52 return re.search(pattern, string, flags)