-1

Original text: This is the first variable "${abc}" and this is the second variable "${def}"

Desired output: This is the first variable and this is the second variable

I want to get rid of "${abc}" and "${def}" using regex. Currently, I am using this regex command: \".*\" but the output I am getting is "${abc}" and this is the second variable "${def}"

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Prog
  • 21

1 Answers1

0

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

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70