0
我有這個字符串:PHP:取決於正則表達式組值取決於行?
[000] Foo
[???] Foo
[020] Foo
[???] Foo
[045] Foo
[???] Foo
[???] Foo
[110] Foo
[???] Foo
我需要檢查[XXX]值,每一行,但它需要智能:
如果???
值0**
行之間>替換到0??
。
期望的結果:
[000] Foo
[0??] Foo
[020] Foo
[0??] Foo
[045] Foo
[???] Foo
[???] Foo
[110] Foo
[???] Foo
我是否需要preg_replace_callback()
?
在接下來的步驟中,我想將其擴展爲「1XX和2XX之間」等邏輯,但我可能能夠適應已發佈的解決方案。
測試:
<?php
// string
$s = '[000] Foo
[???] Foo
[020] Foo
[???] Foo
[045] Foo
[???] Foo
[???] Foo
[110] Foo
[???] Foo';
// _preg_replace magic
// output
echo 'After replacement:';
echo '<pre>' . $s . '</pre>';
echo 'Desired result:';
echo '<pre>[000] Foo
[0??] Foo
[020] Foo
[0??] Foo
[045] Foo
[???] Foo
[???] Foo
[110] Foo
[???] Foo</pre>';
?>
嘗試['preg_replace('〜^ \ [\?{3}(?=]。*?\ R \ [0)〜sm','[0 ??',$ s)']( https://regex101.com/r/eV5GRr/1) –
哇,謝謝。不知道它做什麼壽:D – Martin
適用於0XX,但它不看行之間。例如。這裏https://regex101.com/r/VII7hy/1我想匹配'[100]'和'[115]' – Martin