1 回答

TA貢獻1824條經(jīng)驗 獲得超6個贊
正如人們已經(jīng)在評論部分指出的那樣:正則表達式可能不是執(zhí)行此操作的最佳解決方案。不管怎樣,你要求它,我在下面的課程中測試了它。
// 1) without class check -> this does not work with code on line with opening {
preg_match('/(?:^{(?!\r?\n?\s*\*\/)|{\s*$(?!\r?\n?\s*\*\/)).+^\s*}(?!\r?\n?\s*\*\/)/ms', $c, $match);
// 2) with class check -> this should always work
preg_match('/^[\s\w]+?(?:{(?!\r?\n?\s*\*\/)|{\s*$(?!\r?\n?\s*\*\/)).+^\s*}(?!\r?\n?\s*\*\/)/ms', $c, $match);
// 3) with class check and capturing the second part (non-class-definition) separately -> this should always work
preg_match('/^[\s\w]+?((?:{(?!\r?\n?\s*\*\/)|{\s*$(?!\r?\n?\s*\*\/)).+^\s*}(?!\r?\n?\s*\*\/))/ms', $c, $match);
我建議使用 3)。
/**
?* The LightTaskSchedulerService class. :{
?*/
class LightTaskSchedulerService implements TaskSchedulerService {
{
? ? /**
? ? ?*
? ? ?* This method IS the task manager.
? ? ?* See the @page(Light_TaskScheduler conception notes) for more details.
? ? ?*
? ? ?*/
? ? public function run()
? ? {
? ? ? ? $executionMode = $this->options['executionMode'] ?? "lastOnly";
? ? ? ? $this->logDebug("Executing run method with execution mode \"$executionMode\".");
? ? ? ? if ($foo) {
? ? ? ? ? ? doBar($foo);
? ? ? ? }
? ? ? ? /* multiline */
? ? ? ? // simple one line comment
? ? ? ? // simple one line comment { }
? ? ? ? # another comment
? ? ? ? # another comment}} {
? ? ? ? # another comment{/*}*/
//}
#}
/*}*/
/*{*/
/*
}*/
/*
}
*/
? ? }
}
// this can happen in comments:}, why
// more stuff
/* multiline hello} hello{
}*/
# singleline{
#}
//}
/*}*/
/**
}*/
輸出:
Array
(
? ? [0] => {
{
? ? /**
? ? ?*
? ? ?* This method IS the task manager.
? ? ?* See the @page(Light_TaskScheduler conception notes) for more details.
? ? ?*
? ? ?*/
? ? public function run()
? ? {
? ? ? ? $executionMode = $this->options['executionMode'] ?? "lastOnly";
? ? ? ? $this->logDebug("Executing run method with execution mode \"$executionMode\".");
? ? ? ? if ($foo) {
? ? ? ? ? ? doBar($foo);
? ? ? ? }
? ? ? ? /* multiline */
? ? ? ? // simple one line comment
? ? ? ? // simple one line comment { }
? ? ? ? # another comment
? ? ? ? # another comment}} {
? ? ? ? # another comment{/*}*/
//}
#}
/*}*/
/*{*/
/*
}*/
/*
}
*/
? ? }
}
)
您的代碼不起作用,因為它有錯誤:
未知修飾符
g
(for?preg_match
) =>preg_match_all
使用$c
在你的代碼中不起作用,因為它不在 php 范圍內(nèi)寫:<?php $c = <<<'EEE' ...
相反在你的案例中,后面的外觀不起作用,因為你不能使用
+*?
修飾符。
參考:
在php.net上,“g”未列為選項。
修飾符“g”:preg_match_all
我認為你甚至不需要preg_match_all
一個簡單的preg_match
應該工作,因為無論如何你只需要這場比賽。
這應該可以工作(用 PHP 7.0.1 測試)。它對我來說:
preg_match('/^class\s+\w+\s*({.+(?<! )})/ms', $c, $match);
// or:
preg_match('/^class[^{]+({.+(?<! )})/ms', $c, $match);
// or even:
preg_match('^{.+\r?\n}(?<! )/ms', $c, $match);
print_r($match);
我的正則表達式中的否定查找后面會檢查本例中后面跟著的前導空格}- 在本例中右括號需要位于最左角。這將起作用,除非您希望以不同的方式進行。無論如何,你需要一個分隔符。而且您也不希望 run() 方法中的 if 語句的右大括號結束搜索。
print_r$match上面第一條語句的輸出preg_match:
Array
(
? ? [0] => class LightTaskSchedulerService
{
? ? /**
? ? ?*
? ? ?* This method IS the task manager.
? ? ?* See the @page(Light_TaskScheduler conception notes) for more details.
? ? ?*
? ? ?*/
? ? public function run()
? ? {
? ? ? ? $executionMode = $this->options['executionMode'] ?? "lastOnly";
? ? ? ? $this->logDebug("Executing run method with execution mode \"$executionMode\".");
? ? ? ? if ($foo) {
? ? ? ? ? ? doBar($foo);
? ? ? ? }
? ? }
}
? ? [1] => {
? ? /**
? ? ?*
? ? ?* This method IS the task manager.
? ? ?* See the @page(Light_TaskScheduler conception notes) for more details.
? ? ?*
? ? ?*/
? ? public function run()
? ? {
? ? ? ? $executionMode = $this->options['executionMode'] ?? "lastOnly";
? ? ? ? $this->logDebug("Executing run method with execution mode \"$executionMode\".");
? ? ? ? if ($foo) {
? ? ? ? ? ? doBar($foo);
? ? ? ? }
? ? }
}
)
- 1 回答
- 0 關注
- 168 瀏覽
添加回答
舉報