| Total Complexity | 3 |
| Total Lines | 21 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import re |
||
| 2 | |||
| 3 | from benedict.core.keypaths import keypaths |
||
| 4 | from benedict.utils import type_util |
||
| 5 | |||
| 6 | |||
| 7 | def match(d, pattern, separator=".", indexes=True): |
||
| 8 | if type_util.is_regex(pattern): |
||
| 9 | regex = pattern |
||
| 10 | elif type_util.is_string(pattern): |
||
| 11 | # all indexes wildcard support |
||
| 12 | pattern = re.sub(r"([\*]{1})", "(.)*", pattern) |
||
| 13 | # escape square brackets |
||
| 14 | pattern = re.sub(r"(\[([^\[\]]*)\])", "\\[\\g<2>\\]", pattern) |
||
| 15 | regex = re.compile(pattern, flags=re.DOTALL) |
||
| 16 | else: |
||
| 17 | raise ValueError(f"Expected regex or string, found: {type(pattern)}") |
||
| 18 | kps = keypaths(d, separator=separator, indexes=indexes) |
||
| 19 | values = [d.get(kp) for kp in kps if regex.match(kp)] |
||
| 20 | return values |
||
| 21 |