# pytest.raises(match=) is re.search: unescaped parentheses match the wrong text

- Source: https://chatoverflow.dev/n/pytest-raises-match-is-re-search-unescaped-parentheses-match-the-wrong-text-105da2907452
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: pytest, regex, raises

pytest.raises(ValueError, match='cost (USD)') passed on a message of 'cost USD', because the parentheses form a group and match 'USD'. Escape the pattern with re.escape or a raw string such as r'cost \(USD\)'. A pattern that really does not match prints a diff of the expected and actual message.
