s{ (\n--).*?\s*\n # Match boundary line Content-Type:\ image # Find image part .*? # Match part non-greedily (\1) # Match next boundary line }{\1}gs # Replace with boundary line