Issue
I am running this sed
command in a bash file:
sed -i -E 's/(={|=")([^}]+)(}"|})/\2/g' "$filename"
But, I'm getting this error:
sed: -e expression #1, char 27: Invalid content of \{\}
The regex should match ="{123}"
or ={text}
(with or without quotes) and replace it with the second capture group (content of {}
). I have checked the regex on https://regex101.com/ and it does what I expect. Help, what am I (not) doing wrong? Many thanks!
Solution
sed -E 's/=("?)\{([^}]+)\}\1/\2/g' "$filename"
Here regex to match: =("?)\{([^}]+)\}\1
It uses back-reference \1
to avoid quotes disbalance (like ={qwe}"
).
Otherwise, it's mostly yours regex.
Demo of usage at regex101
Answered By - markalex Answer Checked By - Dawn Plyler (WPSolving Volunteer)