我想捕獲在父母中用括號包裹的重復值。
(parent (foo 25) (bar 500) (child (100 10) (300 20) (400 30)))
(parent (foo 80) (bar 130) (child (200 15)))
知道每個孩子可以包含一個或多個孩子(child (..) ..),或者(child (..) (..) (..) ..)
格式化:
(parent
(foo 25)
(bar 500)
(child
// want to capture the below children dynamically (the integers only)
(100 10)
(300 20)
(400 30)
)
)
// without conflicting with other values in same file
(extra (foo 18) (bar 77))
(different (foo 46) (bar 190))
我試圖得到的輸出:
Array(
'100 10',
'300 20',
'400 30'
)
不確定包括我嘗試過的東西,它們都不是我需要的。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
假設元素(100 10)只會作為子元素出現,那么在這里使用正則運算式查找所有方法可能是可行的:
$input = "(parent (foo 25) (bar 500) (child (100 10) (300 20) (400 30)))";
$input = preg_replace("/\(parent (?:\((?!\bchild\b)\w .*?\) )*/", "", $input);
preg_match_all("/\((\d \d )\)/", $input, $matches);
print_r($matches[1]);
這列印:
Array
(
[0] => 100 10
[1] => 300 20
[2] => 400 30
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/469173.html
上一篇:正則運算式忽略引導字符集的單詞
