如何使用正则表达式排除两个大括号之间的文本?

How to exclude text between two curly brackets with regex?

我是正则表达式的新手,我有这样一段文字:

test{{this should not be selected and the curly brackets too}} but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets.

我想要这个结果

"test"

"but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets."

这是我使用的表达方式:

$p = '/[a-zA-Z0-9#\' ]+(?![^{{]*}})/';

但这不包括单个大括号。
我想知道如何在文本中包含单个大括号并仅排除两个大括号之间的文本
你能给我一些关于正则表达式的好的文档吗?我想了解更多。

使用 preg_replace 并将出现的所有 \{\{[^\}]*\}\} 替换为空字符串。

示例:http://www.regextester.com/?fam=97777

解释:

\{      - {
\{      - {
[^\}]*  - everything except }
\}      - }
\}      - }
(?:^|(?:}}))(.+?)(?:$|{{)

试一试:https://regex101.com/r/2Xy7gU/1/
这里发生了什么:

  • (?:^|(?:}})) - 它以字符串开头或 }}
  • 开头
  • (.+?) - 匹配所有内容(不贪心)
  • (?:$|{{) - 必须匹配 以字符串的任一端或 {{
  • 结尾

你要的(不带括号)在第一组。

2 个选项:

  • 简单:只需将 {{ }} 之间的块视为拆分模式
    $validblocks = preg_split("/{{[\w .]+}}/", $str);
  • 复杂:使用组并首先捕获拒绝的模式,然后剩下的:
    (?<novalid>{{[\w ]+}})|(?<valid>{|[\w .]*|})
    之后根据需要进行管理。此处示例:https://regex101.com/r/SK729o/2

输入(为了效果我把字符串加倍了):

$string = 'test{{this should not be selected and the curly brackets too}} but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets. test{{this should not be selected and the curly brackets too}} but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets.';

方法一preg_split():

var_export(preg_split('/{{[^}]*}}/', $string, 0, PREG_SPLIT_NO_EMPTY));
// Added the fourth param in case the input started/ended with a double curly substring.

方法 #2 preg_match_all():

var_export(preg_match_all('/(?<=}{2}|^)(?!{{2}).*?(?={{2}|$)/s', $string, $out) ? $out[0] : []);

输出(任一方式):

array (
  0 => 'test',
  1 => ' but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets. test',
  2 => ' but this one { or } should be selected. So I want to exclude all text between an opening and closing curly brackets.',
)

preg_split() 将双卷曲包裹的子字符串视为“定界符”并在其上拆分完整的字符串。


preg_match_all() 方法模式... Pattern Demo 这使用正向后视和正向前视,两者都寻找双卷曲或 start/end 字符串。它在中间使用负先行以避免在新行的开头匹配不需要的双卷曲字符串。最后,模式末尾的 s 修饰符将允许 . 匹配换行符。