简体   繁体   English

使用正则表达式从文本中只找到双花括号单词

[英]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 {{abc} and {opq}} except {{xyz}} and {{bpq}} .从这段文字中,我只想找到{{abc}{opq}}除了{{xyz}}{{bpq}} what will be the regular expression for php . php的正则表达式是什么。

I have tried this我试过这个

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

You can use this regex in PHP with PCRE verb (*SKIP)(*F) which is used to skip+fail a match in alternation.您可以在 PHP 中使用此正则表达式,并带有 PCRE 动词(*SKIP)(*F) ,它用于交替跳过+失败匹配。

{{[^{}]*}}(*SKIP)(*F)|{{[^{}]*}|{[^{}]*}}

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 { (?<!})如果前一个字符是 {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM