Masum Masum - 1 year ago 89
PHP Question

Using Regex find only double curly braces word from text

I have a text like this

Hello {{xyz}} how are {{abc}. I am {opq}} for your {{bpq}}.

from this text i want to find only
. what will be the regular expression for

I have tried this

preg_match_all("/\{{(.*?)\}}/", $mytext, $matches);

Answer Source

You can use this regex in PHP with PCRE verb (*SKIP)(*F) which is used to skip+fail a match in alternation.


RegEx Demo

Alternatively, you can also use a lookaround based regex:

  • (?!}) is negative lookahead to fail the match if next char is }
  • (?<!}) is negative lookbehind to fail the match if previous char is {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download