Wipster - 6 months ago 23

PHP Question

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.

Answer

You can use this lookahead based regex:

```
$re = '/[{}](?![^$\s]*\$)/';
$str = "Lorem Ipsum \$\mathbb{R}\$ dolore. {Author} \${1,\dotsc,n}\$";
$result = preg_replace($re, '', $str);
//=> Lorem Ipsum $\mathbb{R}$ dolore. Author $\{1,\dotsc,n}$
```

`(?![^$\s]*\$)`

is negative lookahead to assert we are not in math-mode by ensuring there is no `$`

ahead following non-space, non-dollar characters.

**Alternatively** you can use PCRE verbs `(*SKIP)(*F)`

to skip the math-mode blocks and replace `{`

and `}`

in rest of the text:

```
$re = '~\$[^$\h]*\$(*SKIP)(*F)|[{}]~';
$result = preg_replace($re, '', $str);
```