Php 在RTF中捕获整个标签

Php 在RTF中捕获整个标签,php,regex,Php,Regex,我有一个在rtf中处理文档工作流的系统。它与标准Rtf配合使用效果很好。我知道正则表达式可以在word 2003中处理它。我希望能够处理word 2007 我的标签如下所示:[[FooBuzz]] 许多像写字板这样的程序都将[[FooBuzz]]保持为纯文本。Word 2003从标记中分解[]。 Word2007甚至是最糟糕的,他每次都会爆发,所以FooBuzz 我的样本数据: { toto}{\rtlch\fcs1 \af0 \ltrch\fcs0 \insrsid5517131 [[}{\r

我有一个在rtf中处理文档工作流的系统。它与标准Rtf配合使用效果很好。我知道正则表达式可以在word 2003中处理它。我希望能够处理word 2007

我的标签如下所示:[[FooBuzz]]

许多像写字板这样的程序都将[[FooBuzz]]保持为纯文本。Word 2003从标记中分解[]。 Word2007甚至是最糟糕的,他每次都会爆发,所以FooBuzz

我的样本数据:

{ toto}{\rtlch\fcs1 \af0 \ltrch\fcs0 \insrsid5517131 [[}{\rtlch\fcs1 \af0 \ltrch\fcs0 \insrsid2708730 Foo}{\rtlch\fcs1 \af0 \ltrch\fcs0 \insrsid2708730 Buzz}{\rtlch\fcs1 \af0 \ltrch\fcs0 \insrsid5517131 ]]} {toto}
我需要两件事。首先,正则表达式as要匹配[[FooBuzz]]

例如: {\rtlch\fcs1\af0\ltrch\fcs0\insrsid5517131[}{\rtlch\fcs1\af0\ltrch\fcs0\insrsid2708730 Foo}{\rtlch\fcs1\af0\ltrch\fcs0\insrsid2708730 Buzz}{\rtlch\fcs1\af0\ltrch\ltrch\fcs0\fcs0\insrsid5517131]}

其次,我想选择标签的名称。这里是foobzz。我必须使用php函数preg_match_all

这是一个测试结果,测试数据是双重的:

Array
( [0]=>阵列 ( [0]=>{\rtlch\fcs1\af0\ltrch\fcs0\insrsid5517131[}{\rtlch\fcs1\af0\ltrch\fcs0\insrsid2708730 Foo}{\rtlch\fcs1\af0\ltrch\fcs0\insrsid2708730 Buzz}{\rtlch\fcs1\afs1\af0\ltrch\fcs0\insrsid5517131]} [1] =>{\rtlch\fcs1\af0\ltrch\fcs0\insrsid5517131[}{\rtlch\fcs1\af0\ltrch\fcs0\insrsid2708730 Foo}{\rtlch\fcs1\af0\ltrch\fcs0\insrsid2708730 Buzz}{\rtlch\fcs1\af0\ltrch\ltrsid2708730 Foo}} )

)

如您所见,它会根据需要生成选项卡。键1是我稍后将处理的错误。键2作为结果,只有在[[FooBuzz]]未分解的情况下。键3作为使用word 2003的结果

所以Foo和Buzz可能在不同的数组中,这对我来说已经足够好了,只要它是一致的

例如:

[3] => Array
    (
        [0] => Foo

    )
 [4] => Array
    (
        [0] => Buzz

    ) 

这些都是公认的答案

我的正则表达式和他的解释:

我收到了有关stackoverflow的帮助来构建它:

/(\[\[([^\[\]]*?)\]\]|{[^{]*?\[\[.*?(?<=\[\[).+?\b(?<!\\)(\w+)\b(?=.+?\]\]).*?\]\].*?})/
您可以自由添加另一个或多个。否则,我认为要编辑的部分是: \b(?使用正则表达式模式

/{[^{]*\[{2}.*?\b(\w+)}.*?(?:\b(\w+)}.*?)?\]{2}[^}]*}/
                   ↑             ↑
                  Foo          Buzz
PHP代码: 测试此代码

/(\[\[([^\[\]]*?)\]\]|{[^{]*?\[\[.*?(?<=\[\[).+?\b(?<!\\)(\w+)\b(?=.+?\]\]).*?\]\].*?})/
/(        Begenning of the OR clause
 \[\[([^\[\]]*?)\]\]   Regex used to catch [FooBuzz] in plain text.
 |   Or statement.
 {[^{]*?\[\[.*?(?<=\[\[).+?  Part able to catch  the Rtf translation of [[
   \b(?<!\\)(\w+)\b     This part have a negative look behind. It match rtf metadata (ex \toto123. And i selects Foo
 (?=.+?\]\]).*?\]\].*?} Match the RTF translations of ]]
 )/ End of or statement.
I want to match all the RTF reprsentation of [[FooBuzz]] with match 1.
I want either match x => FooBuzz or match x => Foo match x + 1 => Buzz, if consistent.
/{[^{]*\[{2}.*?\b(\w+)}.*?(?:\b(\w+)}.*?)?\]{2}[^}]*}/
                   ↑             ↑
                  Foo          Buzz
$pattern = '/{[^{]*\[{2}.*?\b(\w+)}.*?(?:\b(\w+)}.*?)?\]{2}[^}]*}/';
preg_match($pattern, $subject, $matches);