字符串到数组,用新行和括号分隔

时间:2018-12-20 22:52:35

标签: php regex preg-match pcre

我有一个很大的字符串块,需要根据它们是否用括号括起来或用换行符分隔成多个数组。

输入:

[this is block
this is also same "block"
this is same block

another same block]
new block!
another new block!
[this is new block
this is also a new block]

我尝试过的许多事情之一:

$block_lines = preg_split('/\[([^]]+)\]|\r/', $block_content);

预期结果:

Array
(
  [0] => 'this is block
  this is also same "block"
  this is same block

  another same block'
  [1] => 'new block!'
  [2] => 'another new block!'
  [3] => 'this is new block
  this is also a new block'
)

实际结果:

Array
(
  [0] => 'new block!'
  [1] => 'another new block!'
  [2] => ''
)

3 个答案:

答案 0 :(得分:2)

您可以在preg_split中使用此正则表达式:

/\[([^]]+)]|\R/

它将字符串拆分为[]内的字符串或换行符。通过使用PREG_SPLIT_DELIM_CAPTURE标志,我们还可以捕获[]的内容:

$string = '[this is block
this is also same "block"
this is same block

another same block]
new block!
another new block!
[this is new block
this is also a new block]';
print_r(preg_split('/\[([^]]+)]|\R/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE));

输出:

Array ( 
    [0] => this is block 
this is also same "block" 
this is same block

another same block
    [1] => new block!
    [2] => another new block!
    [3] => this is new block
this is also a new block
)

Demo on 3v4l.org

答案 1 :(得分:1)

首先匹配所有方括号(可能包含换行),否则匹配单行。

在这种情况下,我偏爱preg_match_all()胜过preg_split()的原因是因为简单来说,您实际上并不是要进行动态爆炸而是要寻找匹配项。

代码:(Demo

$text = '[this is block
this is also same "block"
this is same block

another same block]
new block!
another new block!
[this is new block
this is also a new block]';

var_export(preg_match_all('~\[[^\]]*]|.+~', $text, $matches) ? $matches[0] : 'nothing');

输出:

array (
  0 => '[this is block
this is also same "block"
this is same block

another same block]',
  1 => 'new block!',
  2 => 'another new block!',
  3 => '[this is new block
this is also a new block]',
)

答案 2 :(得分:0)

或者,要更改逻辑的措词,您希望爆炸所有不在方括号表达式内的换行符。您可以使用set()来匹配和忽略括号中的表达式,并在所有通过过滤器的换行符上爆炸。

代码:(Demo

update()

输出:

(*SKIP)(*FAIL)