Php 在新行之前匹配固定数量的零
我需要匹配固定数量的零(即字符串),后跟新行,然后从字符串中删除它 这就是我正在研究的模式:Php 在新行之前匹配固定数量的零,php,regex,Php,Regex,我需要匹配固定数量的零(即字符串),后跟新行,然后从字符串中删除它 这就是我正在研究的模式: 000000000000000 000000000000000 000000000000000 000100000001000 000010000010000 000001000100000 001000111000100 000100010001000 000010010010000 000001010100000 001000111000100 000100010001000 00001001001
000000000000000
000000000000000
000000000000000
000100000001000
000010000010000
000001000100000
001000111000100
000100010001000
000010010010000
000001010100000
001000111000100
000100010001000
000010010010000
000001010100000
000000111000000
001111111111100
000000010000000
000000010000000
000000010000000
000000010000000
000000010000000
000000010000000
000000010000000
000000000000000
000000000000000
PHP代码是:
$regex = '/^[0{' . $amount . '}]\n$/m'; // /^[0{15}]\n$/m
$pattern = preg_replace($regex, null, $pattern); // $pattern is pattern above
您不需要regexp中的
$
(因为\n
之后还有数字)。方括号用于“其中之一”,这里不需要它们。我还怀疑您是否需要“在第一个位置”标记^
。和replacer应该是一个字符串:
$regex = '/0{' . $amount . '}\n/m'; // /^[0{15}]\n/m
$pattern = preg_replace($regex, '', $pattern); // $pattern is pattern above
修正了模式和代码解析非常糟糕的问题