[英]Regex: capture paired curly braces
I want to capture matched curly braces. 我想捕捉匹配的花括号。
For example : 例如 :
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 {}
Expected output: 预期产量:
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 {}
ie I want to capture the text between \\added[]{
and }
(its relative closing curly braces).Problem with my regex is, I don't know how to capture between the related curly braces. 即我想捕获\\added[]{
和}
(它的相对闭合花括号)之间的文本。问题与我的正则表达式是,我不知道如何捕获相关的花括号之间。
I tried, 我试过了,
"/\\\\added\\[.*?\\]{(.[^{]*?)}/s"
I know it ignores if {
present in the text. 我知道它会忽略{
如文中所示。 But I don't get an idea how to create a regex to get matched curly braces alone. 但我不知道如何创建一个正则表达式来单独匹配花括号。
To match paired braces you'll want to use a recursive subpattern . 要匹配成对的大括号,您需要使用递归子模式 。
Example: 例:
$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";
}
Output: 输出:
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 {}
Here, should work 在这里,应该工作
/\\added\[.*\]\{(.*(?:.*\{.*\}.*)*)\}/gU
Explanation 说明
/\\\\added\\
is a Latex tag, /\\\\added\\
是一个Latex标签,
\\[.*\\]
is an option of Latex tag, \\[.*\\]
是Latex标签的一个选项,
\\{
open bracket, \\{
open bracket,
(.*(?:.*\\{.*\\}.*)*)
is captured text which here we also prevent for recursive {...}
or multiple {...}
inside our target tag, (.*(?:.*\\{.*\\}.*)*)
是捕获的文本,在这里我们也阻止在我们的目标标记内递归{...}
或多个{...}
,
\\}
close bracket. \\}
关闭括号。
Strategy 战略
I do not consider pair of bracket as a recursive form 我不认为对括号作为递归形式
{ { {...} } }
c b a a b c
where we have pair a
, b
and c
, 我们有a
, b
和c
,
but I consider them like this! 但我认为他们是这样的!
{ { {...} } }
a b c a b c
The last two examples in my demo also prove that it work correctly. 我的演示中的最后两个示例也证明它可以正常工作。
IMPORTANT: the modifier U
suppose to be used here for a purpose of non-greedy quantifier otherwise my regex will not work correctly. 重要提示:修饰符U
在这里用于非贪婪量词的目的,否则我的正则表达式将无法正常工作。
使用以下正则表达式:
\\\\added\\[[^\\]]\*][^\\{]\*{((?:(?:[^\\{\\}]\*\\{[^\\}\\{]\*\\})\*||[^\\}]\*)\*)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.