第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一個正則表達式來匹配 php 類的開始和結束字符內(nèi)的內(nèi)容

一個正則表達式來匹配 php 類的開始和結束字符內(nèi)的內(nèi)容

PHP
暮色呼如 2023-08-19 16:39:06
我無法找到與 php 類的開始和結束字符(分別為 { 和 })匹配的正則表達式。如果 { 和 } 位于 php 注釋內(nèi),正則表達式也不應該匹配它們,換句話說,如果 { 或 } 前面有除空格之外的任何字符,則它不應該匹配。我想我應該使用負向查找,但我對正則表達式有點生疏,到目前為止我還沒有找到解決方案。這是我的測試字符串:<?phpnamespace Ling\Light_TaskScheduler\Service;/** * The LightTaskSchedulerService class. :{ */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\".");    }}// this can happen in comments: }, why// more stuff我的模式目前不起作用,是這樣的:    if(preg_match('!^\s*\{\s*(.*)(?<![^\s]*)\}!ms', $c, $match)){        a($match);    }所以,我使用了多行修飾符“m”,因為我們需要解析多行字符串,然后我使用了“s”修飾符,以便點與換行符匹配,但然后在部分 (?<![^\s ]*) 似乎不起作用。我基本上是想說,如果“}”字符前面沒有空格,則不匹配它。@Wiktor Stribi?ew:我嘗試了這種模式,但它仍然不起作用:!^\s*\{\s*(.*)(?<!\S)\}!ms考慮到 Tim Biegeleisen 的評論,我可能會采取一種更簡單的方法,例如先刪除注釋,然后執(zhí)行更簡單的 regex !^\s*\{\s*(.*)\}!ms,我知道這會起作用。但是,如果有人知道可以執(zhí)行此操作的正則表達式,我將有興趣查看它。
查看完整描述

1 回答

?
慕妹3242003

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{/*}*/

//}

#}

/*}*/

/*{*/

/*

}*/

/*

}

*/

? ? }

}

)

您的代碼不起作用,因為它有錯誤:

  1. 未知修飾符g(for?preg_match) =>preg_match_all使用

  2. $c在你的代碼中不起作用,因為它不在 php 范圍內(nèi)寫:<?php $c = <<<'EEE' ...相反

  3. 在你的案例中,后面的外觀不起作用,因為你不能使用+*?修飾符。

參考:

在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);

? ? ? ? }

? ? }

}

)


查看完整回答
反對 回復 2023-08-19
  • 1 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號