我有如下格式的字符串:
$string ='[(id,name,top,left,width,height,depth)name|name]
[(id,name,top,left,width,height,depth)]
[(id,name,top,left,width,height,depth)name|name]
[(id,name,top,left,width,height,depth)]
[(id,name,top,left,width,height,depth)name]';
如果字符串的一部分不包含name
,则从字符串中删除该部分。
所以输出会像
$output ='[(id,name,top,left,width,height,depth)name|name]
[(id,name,top,left,width,height,depth)name|name]
[(id,name,top,left,width,height,depth)name]';
我试过这样的事情
$re = '/\[\((.*?)\)(.*?)\]/s';
但没有按预期工作
您需要找到每个字符串,其中:
[
和(
。)
(括号之间的内容)以外的一系列字符,)
并且]
它们之间没有任何字符(名称)并将其替换为空字符串。
所以,正则表达式应该是:\s*\[\([^)]+\)\]
。
请注意,这\n
也是一个白色字符,因此实际上已删除:
但终止该行的换行符(如果有)不会被删除。
有关工作示例,请参阅https://regex101.com/r/NVGild/1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句