簡體   English   中英

在Regex PHP中捕獲重復的組

[英]Capturing repeated group in Regex PHP

我正在嘗試捕獲PHP正則表達式中的重復組。

我正在使用的表達式是: (?:.*\\s|^)@\\[(\\d+)\\] ,它將匹配以下內容:

some text here @[2] some more text並在比賽中返回2。

現在我要匹配:

some text here @[2] some more text @[3] more text然后返回[2,3]。 現在,我只能用以下表達式返回3(最后一個元素): (((?:.*\\s|^)@\\[(\\d+)\\])+)

我已經讀過這個這個 ,但是我無法讓那些使用preg_match或preg_match_all的人工作。

任何對此的投入將不勝感激。

我建議這個更簡單的解決方案:

$string = 'some text here @[2] some more text @[3] more text';
preg_match_all('/@\[(\d+)\]/', $string, $matches);
$matches = $matches[1];
$result = '[' . implode(', ', $matches) . ']';
echo $result; // [2, 3]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM