I have a CSS file which includes a set of rules wrapped in a @critical rule containing critical styles whose content I want to extract for inlining:
@critical {
.foo {
...
}
@media bar {
...
}
}
I’m trying to do the extraction using sed search and replace with the following regex (I’m on a Mac, hence the -E flag):
sed -i '' -E 's,@critical[^{]*{\s*((.|\s)*)[^}]*},\1,g' style.css
Effectively I'm trying to replace the entire @critical rule with only its content (match group 1). The regex critical[^{]*{\s*((.|\s)*)[^}]*} itself works perfectly when I plug it into RegExr but I can’t seem to get it to work in the context of the above sed command. What am I doing wrong?
Thanks very much in advance.
—————
Edit:
I have since learned that sed doesn’t support multiline regex which would explain the problem I’m encountering. While minifying the CSS (effectively turning it into a single line of text) makes it work under sed, I’d still appreciate someone pointing me towards a solution that works across a non-minified multiline CSS file (perl? awk?).