Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions benedict/core/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def match(
pattern = re.sub(r"([\*]{1})", "(.)*", pattern)
# escape square brackets
pattern = re.sub(r"(\[([^\[\]]*)\])", "\\[\\g<2>\\]", pattern)
# anchor to end so the full keypath must be consumed
pattern = pattern + "$"
regex = re.compile(pattern, flags=re.DOTALL)
else:
raise ValueError(f"Expected regex or string, found: {type(pattern)}")
Expand Down
11 changes: 11 additions & 0 deletions tests/core/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def test_match_with_regex_pattern(self) -> None:
]
self.assertEqual(values, expected_values)

def test_match_with_suffix_wildcard(self) -> None:
# Suffix wildcard like "*.jpg" must not match "IMG_0001.jpg.bak" –
# i.e. the full keypath must be consumed, not just a prefix.
d = {
"IMG_0001.jpg": "IMG_0001.jpg",
"IMG_0001.jpg.bak": "IMG_0001.jpg.bak",
"DOC_0001.pdf": "DOC_0001.pdf",
}
values = _match(d, "*.jpg")
self.assertEqual(values, ["IMG_0001.jpg"])

def test_match_with_invalid_pattern(self) -> None:
d = self._get_dict()
with self.assertRaises(ValueError):
Expand Down