如何处理regex中的内部标记?
我尝试用正则表达式(PHP如何处理regex中的内部标记?,regex,preg-match-all,Regex,Preg Match All,我尝试用正则表达式(PHPpreg\u match\u all)解析CSS,比如 但是问题是当有一个内部的{标记时 table { border-collapse: collapse; border-spacing: 0 } @keyframes blinking { from { background: #f4a83d } to { background: rgba(244, 168, 61, 0) } } 我
preg\u match\u all
)解析CSS,比如
但是问题是当有一个内部的{
标记时
table {
border-collapse: collapse;
border-spacing: 0
}
@keyframes blinking {
from {
background: #f4a83d
}
to {
background: rgba(244, 168, 61, 0)
}
}
我需要将其解析为两个元素,但结果是
Array
(
[0] => Array
(
[0] =>
table {
border-collapse: collapse;
border-spacing: 0
}
[1] =>
@keyframes blinking {
from {
background: #f4a83d
}
[2] =>
to {
background: rgba(244, 168, 61, 0)
}
)
[1] => Array
(
[0] =>
table
[1] =>
@keyframes blinking
[2] =>
to
)
[2] => Array
(
[0] => border-collapse: collapse;
border-spacing: 0
[1] => from {
background: #f4a83d
[2] => background: rgba(244, 168, 61, 0)
)
)
注意:有几个类和程序用于解析CSS
,但是它们对于我只需要捕获选择器和规则的内容来说是一种过度的操作。它们解析所有规则,我需要重新加入。因此,使用简单的正则表达式方法更方便。使用
([^{]+)\s*({\s*((?:[^{}]++}(?2))*)\s*)
看
解释
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^{]+ any character except: '{' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
{ '{'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
[^{}]++ any character except: '{', '}' (1 or
more times (matching the most amount
possible possessively))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
(?2) Capturing group 2 pattern recursed
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
} '}'
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^{]+ any character except: '{' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
{ '{'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
[^{}]++ any character except: '{', '}' (1 or
more times (matching the most amount
possible possessively))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
(?2) Capturing group 2 pattern recursed
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
} '}'
--------------------------------------------------------------------------------
) end of \2