Reputation: 1570
I want to have curly braces removed outside texs math-mode. For example:
Lorem Ipsum $\mathbb{R}$ dolore. {Author} $\{1,\dotsc,n}$
should become:
Lorem Ipsum $\mathbb{R}$ dolore. Author $\{1,\dotsc,n}$
As you can't really negate regular expressions I was looking into look-aheads and -behinds. That wasn't working for me as technically speaking, {Author} also is between two dollar signs. Some regex professional having some advice for me?
I'd love to only use preg_replace when the problem isn't too complex for this.
Upvotes: 1
Views: 78
Reputation: 785058
You can use this lookahead based regex:
$re = '/\$\\\w*{[^}]+}(*SKIP)(*F)|{[^}]*}/';
$str = "Lorem Ipsum \$\mathbb{R}\$ dolore. {Author} \${1,\dotsc,n}\$";
$result = preg_replace($re, '', $str);
//=> Lorem Ipsum $\mathbb{R}$ dolore. Author $\{1,\dotsc,n}$
Here we are using PCRE verbs (*SKIP)(*F)
to skip the math-mode blocks and replace {
and }
in rest of the text:
Upvotes: 1