Reputation: 127
I've found many examples on the internet, but still can't find a solution for this. I got a PHP page that needs to parse a HTML+JS page and needs to remove everything included between { } and also the brackets itself. The point is that the content of the brackets must be [A-Za-z0-9_] and not anything else (like i did so far).
This is my current replace:
$this->content = preg_replace("/\{((?:[^{}]++|\{(?1)\})++)\}/", "", $this->content);
So far now this works pretty good, the point is that doesn't search for right contents between brackets and removes them no matter what's inside (atm it's removing JS functions from the content of the page that is parsing).
What do I've to modify to add that kind of character restriction to the content of the reg-ex?
Thanks
Upvotes: 2
Views: 7318
Reputation: 16440
The pattern to match those brace substrings is pretty simple:
\{[a-zA-Z0-9_]+\}
It escapes {
and }
which might otherwise be treated as parts of a quantifier (like e.g. {3}
). Note that, depending on the programming language, you might have to double-escape the braces (\\{[a-zA-Z0-9_]+\\}
) so that the backslashes themselves are escaped.
Upvotes: 1
Reputation: 39365
A-Za-z0-9_
can be represented as \w
in regex.
So, if your brackets needs to contain at least a character inside, then the regex will be
\{\w+\}
If you think there can be empty, for example {}
then you can use
\{\w*\}
If you want to allow space as well, then it will be
\{[\w\s]+\}
Upvotes: 1
Reputation: 12306
Try to use this pattern:
\{[a-zA-Z0-9_]+?\}
Better to use lazy quantification with ?
sign at the end of repetitions number.
Upvotes: 3