使用REGEX匹配功能的主体
Given a dummy function as such:
public function handle()
{
if (isset($input['data']) {
switch($data) {
...
}
} else {
switch($data) {
...
}
}
}
My intention is to get the contents of that function, the problem is matching nested patterns of curly braces {...}
.
I've tried the following (no recursion):
$pattern = "/function\shandle\([a-zA-Z0-9_\$\s,]+\)?". // match "function handle(...)"
'[\n\s]?[\t\s]*'. // regardless of the indentation preceding the {
'{([^{}]*)}/'; // find everything within braces.
preg_match($pattern, $contents, $match);
That pattern doesn't match at all.我相信这是最后的位,错误'{([^{}]*)}/'
,因为当身体内没有其他牙套时,该模式有效。
By replacing it with:
'{([^}]*)}/';
It matched till the closing }
of the switch inside the if
statement and stopped there (including }
of the switch but excluding如果)的)。
以及这种模式,同样的结果:
'{(\K[^}]*(?=)})/m';
Given a dummy function as such:
public function handle()
{
if (isset($input['data']) {
switch($data) {
...
}
} else {
switch($data) {
...
}
}
}
My intention is to get the contents of that function, the problem is matching nested patterns of curly braces {...}
.
I've come across recursive patterns but couldn't get my head around a regex that would match the function's body.
I've tried the following (no recursion):
$pattern = "/function\shandle\([a-zA-Z0-9_\$\s,]+\)?". // match "function handle(...)"
'[\n\s]?[\t\s]*'. // regardless of the indentation preceding the {
'{([^{}]*)}/'; // find everything within braces.
preg_match($pattern, $contents, $match);
That pattern doesn't match at all. I am sure it is the last bit that is wrong '{([^{}]*)}/'
since that pattern works when there are no other braces within the body.
By replacing it with:
'{([^}]*)}/';
It matched till the closing }
of the switch inside the if
statement and stopped there (including }
of the switch but excluding that of the if
).
As well as this pattern, same result:
'{(\K[^}]*(?=)})/m';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新#2
根据其他注释
注意:一个简短的正则
{((?> [^{}] ++ |(?r))*)代码>就足够了,如果您知道输入不包含
{
或}
在PHP语法中。,则是一个长的言论。 /em>案例起作用吗?
[ { >在注释块中。
<<<<< str
或<<<<< [''“”] str ['] str [']
否则我们的嵌套括号和嵌套的深度不重要
否则,
您有一个居住在您的代码中的火星,
除非 @sln的 regexformatter 软件。
在live demo中提供了什么?
我 https://raw.githubusercontent.com/illuminate/database/master/master/eloquent/model.php“ rel =“ noreferrer”> model.php.php file(〜3500行)随机给出作为输入。 :
实时演示
Update #2
According to others comments
Note: A short RegEx i.e.
{((?>[^{}]++|(?R))*)}
is enough if you know your input does not contain{
or}
out of PHP syntax.So a long RegEx, in what evil cases does it work?
[{}]
in a string between quotation marks["']
[{}]
in a comment block.//...
or/*...*/
or#...
[{}]
in a heredoc or nowdoc<<<STR
or<<<['"]STR['"]
Otherwise it is meant to have a pair of opening/closing braces and depth of nested braces is not important.
Do we have a case that it fails?
No unless you have a martian that lives inside your codes.
Formatting is done by @sln's RegexFormatter software.
What I provided in live demo?
Laravel's Eloquent Model.php file (~3500 lines) randomly is given as input. Check it out:
Live demo
这可以使输出标头文件(.h)输出内联功能块(.c)
查找正则表达式:
替换为:
输入:
将输出:
以第二匹配的模式获取功能块的主体:将
输出:将输出:
This works to output header file (.h) out of inline function blocks (.c)
Find Regular expression:
Replace with:
For input:
will output:
Get the body of the function block with second matched pattern :
will output: