I’m looking for a regex in order to transform something like
{test}hello world{/test} and {again}i'm coming back{/again} in hello world i'm coming back.
I tried {[^}]+} but with this regex, I can’t have only what I have in the test and again tags. Is there a way to complete this regex ?
Doing this properly is generally beyond the capabilities of regular expressions. However, if you can guarantee that those tags will never be nested and your input will never contain curly brackets that do not signify tags, then this regex could do the matching:
Explanation:
So this uses a backreference
\1to make sure that the closing tag contains the same word as the opening tag. Then you will find the tag’s name in capture1and the tag’s value/content in capture2. From here you can do with these whatever you want (for instance, put the values back together).Note that you should use the
SINGLELINEorDOTALLoption, if you want your tags to span multiple lines.