【问题标题】:Regex: capture paired curly braces正则表达式:捕获成对的花括号
【发布时间】:2015-09-01 10:22:58
【问题描述】:

我想捕获匹配的花括号。

例如

Some example text with \added[author]{text with curly braces{some text}..}

Some example text with \added[author]{text without curly braces}

Some example text with \added[author]{text with {}and {} and {}curly braces{some text}..}

Some example text with \added[author]{text with {}and {} and {}curly braces{some text}..} and extented text with curly braces {}

预期输出:

Some example text with text with curly braces{some text}..

Some example text with text without curly braces

Some example text with text with {}and {} and {}curly braces{some text}..

Some example text with text with {}and {} and {}curly braces{some text}.. and extented text with curly braces {}

即我想捕获\added[]{}(它的相对闭合花括号)之间的文本。我的正则表达式的问题是,我不知道如何在相关的花括号之间进行捕获。

我试过了,

       "/\\\\added\\[.*?\\]{(.[^{]*?)}/s"

如果{ 出现在文本中,我知道它会忽略。但我不知道如何创建一个正则表达式来单独获取匹配的花括号。

【问题讨论】:

标签: php regex


【解决方案1】:

要匹配成对的大括号,您需要使用recursive subpattern


示例:

$regex = <<<'REGEX'
/
\\added\[.*?\]                # Initial \added[author]

(                             # Group to be recursed on.
    {                         # Opening brace.

    (                         # Group for use in replacement.

        ((?>[^{}]+)|(?1))*    # Any number of substrings which can be either:
                              # - a sequence of non-braces, or
                              # - a recursive match on the first capturing group.
    )

    }                         # Closing brace.
)
/xs
REGEX;

$strings = [
    'Some example text with \added[author]{text with curly braces{some text}..}',
    'Some example text with \added[author]{text without curly braces}',
    'Some example text with \added[author]{text with {}and {} and {}curly braces{some text}..}',
    'Some example text with \added[author]{text with {}and {} and {}curly braces{some text}..} and extented text with curly braces {}'
];

foreach ($strings as $string) {
    echo preg_replace($regex, '$2', $string), "\n";
}

输出:

Some example text with text with curly braces{some text}..
Some example text with text without curly braces
Some example text with text with {}and {} and {}curly braces{some text}..
Some example text with text with {}and {} and {}curly braces{some text}.. and extented text with curly braces {}

【讨论】:

  • 完美!太感谢了。你能解释一下这个正则表达式吗?
【解决方案2】:

在这里,应该可以工作

/\\added\[.*\]\{(.*(?:.*\{.*\}.*)*)\}/gU

说明

/\\added\ 是乳胶标签,

\[.*\]是Latex标签的一个选项,

\{ 左括号,

(.*(?:.*\{.*\}.*)*) 是捕获的文本,在这里我们还防止在我们的目标标签中出现递归 {...} 或多个 {...}

\} 右括号。​​

策略

我不认为一对括号是一种递归形式

{ { {...} } }
c b a   a b c

我们有一对 abc

但我认为他们是这样的!

{ { {...} } }   
a b c   a b c

见:DEMO

我演示中的最后两个示例也证明它可以正常工作。

重要提示:修饰符 U 假设在此处用于非贪婪量词,否则我的正则表达式将无法正常工作。

【讨论】:

  • 有效!谢谢你。我应该知道哪个正则表达式是有效的方法吗?有或没有递归?
  • 我的正则表达式甚至不需要lookaround,只是一个简单的模式。希望你满意!
  • 我将自己使用一个简单的正则表达式模式
  • @Learning 我添加了一些我用来解决这个问题的想法,希望它可以帮助你更多地了解我的正则表达式。
  • 那么,这就像计算打开和关闭的花括号一样吗?
【解决方案3】:

使用以下正则表达式:

\\\\added\\[[^\\]]\*][^\\{]\*{((?:(?:[^\\{\\}]\*\\{[^\\}\\{]\*\\})\*||[^\\}]\*)\*)}

【讨论】:

  • 如果Some example text with \added[author]{text with {}and {} and {}curly braces{some text}..} and extented text with curly braces {} 怎么办?在这种情况下,输出应该是,Some example text with text with {}and {} and {}curly braces{some text}.. and extented text with curly braces {} 问题是,它捕获到最后一个大括号,直到匹配的大括号才捕获
【解决方案4】:

使用这个regex

/\\added[^]]*]{([^}]*}[^}]*)}/s

Demo here

【讨论】:

  • 谢谢!它仅适用于我给出的示例..但正则表达式的目的是获得匹配的花括号。请参阅我更新的帖子以获取更多示例。
猜你喜欢
  • 2020-02-21
  • 2015-01-20
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
相关资源
最近更新 更多