Reputation: 41
I'm trying to capture the characters between curly brackets (including the curly brackets) if part of the characters match.
I've tried the regex pattern below, but it grabs everything starting with the first curly bracket and last curly bracket in the entire string.
string = "The {name_of_list} list contains {list:a, b, and c}. This list should be formatted as a, b, and c."
r"(\{.*?:a, b, and c\})"gm
I'd like to capture "{list:a, b, and c}", but instead I'm getting "{name_of_list} list contains {list:a, b, and c}".
Upvotes: 1
Views: 910
Reputation: 626747
The .
pattern matches any char but line break chars, that is why you get unexpected results.
To make it match what you need you need to "temper" the dot, and here, it is best done with a negated character class, [^{]
(any char but {
) or [^{}]
(any char but {
and }
):
import re
s = "The {name_of_list} list contains {list:a, b, and c}. This list should be formatted as a, b, and c."
print(re.findall(r'\{[^{}]*?:a, b, and c}', s))
See the regex demo and a Python demo.
To match any strings inside curly braces that contain :
, you may use
r'\{[^{}:]*:[^{}]*}'
See the :
included into the first negated character class that lets us use a greedy *
quantifier with it to make it more efficient.
Upvotes: 1