如果一对方括号内有逗号和空格,我试图用一个右方括号和一个开口方括号替换它。
基本上,我有一个看起来像这样的文本块:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi laoreet
lobortis blandit. Proin in nulla mauris.[3, 8, 19, 45] Aenean dictum
sollicitudin nibh, non semper mauris semper id.[52, 58] Donec at varius eros.
Sed in risus nec tellus vehicula semper. In id elit vel nunc ornare faucibus.
Integer feugiat erat mauris, in tempus leo imperdiet ac.[23]
我正试图让它看起来像这样:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi laoreet
lobortis blandit. Proin in nulla mauris.[3][8][19][45] Aenean dictum
sollicitudin nibh, non semper mauris semper id.[52][58] Donec at varius eros.
Sed in risus nec tellus vehicula semper. In id elit vel nunc ornare faucibus.
Integer feugiat erat mauris, in tempus leo imperdiet ac.[23]
我编写了一个preg_replace
函数,但只有在括号集中存在一次才会执行替换:
$output = preg_replace("/(\[\d*), (\d*\])/", "$1][$2", $input);
如何修改我的正则表达式,以便在匹配的文本中执行多次替换?