You want to match all characters that are not "
, so instead of .*
do [^"]*
.
>>> in_str = 'This is the first variable "${abc}" and this is the second variable "${def}"'
>>> re.sub(r'"[^"]*"', '', in_str)
'This is the first variable and this is the second variable '
Better yet, constrain it more so you only match "${...}"
, and not anything enclosed in quotes (r'"\$\{[^}]*\}"'
)
>>> re.sub(r'"\$\{[^}]*\}"', '', in_str)
'This is the first variable and this is the second variable '
Explanation:
"\$\{[^}]*\}"
-------------
" " : Quotes
\$ : A literal $ sign
\{ \} : Braces
[^}]* : Zero or more characters that are not }
To get rid of the trailing spaces after the match, add an optional \s?
at the end of the regex.
>>> re.sub(r'"\$\{[^}]*\}"\s?', '', in_str)
'This is the first variable and this is the second variable '
Of course, this leaves behind a trailing space if the last word was a match, but you can just .strip()
that out.
Try it on Regex101