正则表达式preg_split由新行

时间:2020-12-04 22:08:53

this is my file:

这是我的档案:

0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0
5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1
5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0
6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0 1.0 5.0
7.0 5.0 1.0 5.0 0.0 7.0 1.0 1.0 2.0 1.0 5.0 6.0 2.0 2.0 5.0
2.0 2.0 6.0 2.0 7.0 0.0 5.0 5.0 6.0 5.0 2.0 5.0 1.0 2.0 5.0
5.0 5.0 5.0 1.0 1.0 5.0 0.0 2.0 6.0 1.0 5.0 7.0 5.0 1.0 6.0
7.0 1.0 5.0 1.0 2.0 2.0 1.0 5.0 6.0 5.0 2.0 6.0 7.0 0.0 5.0
5.0 7.0 6.0 5.0 5.0 5.0 6.0 2.0 6.0 2.0 1.0 5.0 6.0 5.0 0.0

I would split it by \n and have returned in one array each row. How can I do the regular expression?

我会将它拆分为\ n并且每行返回一个数组。我该怎么做正则表达式?

$rows = preg_split('$regular_expression', $content);

After I will extract all the rows, how can I extract each value separated by backspace?

在我将提取所有行之后,如何提取由退格分隔的每个值?

$values_in_a_row = preg_split('$regular_expression', $a_row);

Here the text were I am trying to do it http://regexr.com?2v23c . Thanks a lot.

这里的文字是我试图做的http://regexr.com?2v23c。非常感谢。

5 个解决方案

#1


6  

No need for regular expressions :

不需要正则表达式:

<?php
$data = explode("\n", $data); // preg_split('#\n#', $data); Please don't
foreach($data as &$row) {
    $row = explode(' ', $row); // preg_split('#\s#', $row); Seriously
}
print_r($data);
?>

#2


22  

If you are having issues because you don't know if each newline is just \n or \r\n or \r then none of the above answers work and a regexp works. What I did was

如果您遇到问题,因为您不知道每个换行是否只是\ n或\ r \ n或\ r \ n那么上述答案都不起作用且正则表达式有效。我做的是

$lines = preg_split("/(\r\n|\n|\r)/",$content);

Then you can use the accepted answer to split the spaces.

然后,您可以使用接受的答案来分割空格。

#3


4  

$rowsapart = preg_split("/\n/",$rowstogether);

$colspart = preg_split("/\s/",$colstogether);

#4


2  

No need for REGEX, use explode() instead:

不需要REGEX,请使用explode()代替:

<?php

    $file = <<<EOF
    0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0
    5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1
    5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0
    6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0 1.0 5.0
    7.0 5.0 1.0 5.0 0.0 7.0 1.0 1.0 2.0 1.0 5.0 6.0 2.0 2.0 5.0
    2.0 2.0 6.0 2.0 7.0 0.0 5.0 5.0 6.0 5.0 2.0 5.0 1.0 2.0 5.0
    5.0 5.0 5.0 1.0 1.0 5.0 0.0 2.0 6.0 1.0 5.0 7.0 5.0 1.0 6.0
    7.0 1.0 5.0 1.0 2.0 2.0 1.0 5.0 6.0 5.0 2.0 6.0 7.0 0.0 5.0
    5.0 7.0 6.0 5.0 5.0 5.0 6.0 2.0 6.0 2.0 1.0 5.0 6.0 5.0 0.0
    EOF;

    $rows = explode("\n", $file);
    print_r($rows);
    echo "\n\n"; //Spacing

    $numbers_in_a_row = explode(" ", $rows[0]);
    print_r($numbers_in_a_row);

?>

Live Example

实例

#5


0  

I would have bet anything that a regular expression would have worked.

我会赌一个正则表达式会起作用的东西。

I tried preg_split with all combinations of:

我尝试了preg_split与以下所有组合:

  • different quotes (" and ')
  • 不同的引号(“和')
  • different escaping (\r or \r)
  • 不同的逃避(\ r或\ r)
  • different specification for character set: [\r\n], (\r|\n|\r\n|\n\r), "\r*\n"
  • 字符集的不同规范:[\ r \ n],(\ r | \ n | \ r \ n | \ n \ r \ n),“\ r * \ n”
  • modifiers to the regexp (#\r\n#ms) - and no modifier, of course
  • regexp的修饰符(#\ r \ n#ms) - 当然没有修饰符
  • in desperation, different regexp delimiters (/\r/, #\r#).
  • 无奈之下,不同的正则表达式分隔符(/ \ r /,#\ r ##)。

None worked. I can't understand why (PHP 7 on Ubuntu 16 "Xenial Xerus"). In the end I knew from hexdump that I had \n in my input (sometimes \r\n), so resorted to explode:

没有用。我无法理解为什么(Ubuntu 16“Xenial Xerus”上的PHP 7)。最后我从hexdump中知道我在输入中有\ n(有时是\ r \ n),因此使用了爆炸:

$rows = array_filter(
    explode("\n", $file['data']),
    'trim'
);

so that the \n would split and the trim would get rid of any extra \r's.

所以\ n会分裂,修剪将摆脱任何额外的\ r \ n。


I am still upvoting Brian's answer because it is correct (and the accepted answer because it worked). If anyone manages to repeat whatever stupid error I must have been doing even if I can't see what could have been going wrong in so simple a regexp and can tell it to me, there bloody will be a bounty on that.

我仍然支持Brian的答案,因为它是正确的(并且接受的答案因为它有效)。如果有人设法重复我一定要做的任何愚蠢的错误,即使我看不出在这么简单的正则表达式中可能出现什么问题而且可以告诉我,血腥将是一个赏金。

#1


6  

No need for regular expressions :

不需要正则表达式:

<?php
$data = explode("\n", $data); // preg_split('#\n#', $data); Please don't
foreach($data as &$row) {
    $row = explode(' ', $row); // preg_split('#\s#', $row); Seriously
}
print_r($data);
?>

#2


22  

If you are having issues because you don't know if each newline is just \n or \r\n or \r then none of the above answers work and a regexp works. What I did was

如果您遇到问题,因为您不知道每个换行是否只是\ n或\ r \ n或\ r \ n那么上述答案都不起作用且正则表达式有效。我做的是

$lines = preg_split("/(\r\n|\n|\r)/",$content);

Then you can use the accepted answer to split the spaces.

然后,您可以使用接受的答案来分割空格。

#3


4  

$rowsapart = preg_split("/\n/",$rowstogether);

$colspart = preg_split("/\s/",$colstogether);

#4


2  

No need for REGEX, use explode() instead:

不需要REGEX,请使用explode()代替:

<?php

    $file = <<<EOF
    0.0 5.0 5.0 6.0 7.0 2.0 5.0 2.0 1.0 5.0 5.0 1.0 2.0 7.1 5.0
    5.0 0.0 5.0 5.0 5.0 2.0 5.0 1.0 5.0 6.0 6.0 6.0 6.0 1.0 7.1
    5.0 5.0 0.0 6.0 1.0 6.0 5.0 5.0 1.0 6.0 5.0 7.0 1.0 5.0 6.0
    6.0 5.0 6.0 0.0 5.0 2.0 1.0 6.0 5.0 6.0 2.0 1.0 2.0 1.0 5.0
    7.0 5.0 1.0 5.0 0.0 7.0 1.0 1.0 2.0 1.0 5.0 6.0 2.0 2.0 5.0
    2.0 2.0 6.0 2.0 7.0 0.0 5.0 5.0 6.0 5.0 2.0 5.0 1.0 2.0 5.0
    5.0 5.0 5.0 1.0 1.0 5.0 0.0 2.0 6.0 1.0 5.0 7.0 5.0 1.0 6.0
    7.0 1.0 5.0 1.0 2.0 2.0 1.0 5.0 6.0 5.0 2.0 6.0 7.0 0.0 5.0
    5.0 7.0 6.0 5.0 5.0 5.0 6.0 2.0 6.0 2.0 1.0 5.0 6.0 5.0 0.0
    EOF;

    $rows = explode("\n", $file);
    print_r($rows);
    echo "\n\n"; //Spacing

    $numbers_in_a_row = explode(" ", $rows[0]);
    print_r($numbers_in_a_row);

?>

Live Example

实例

#5


0  

I would have bet anything that a regular expression would have worked.

我会赌一个正则表达式会起作用的东西。

I tried preg_split with all combinations of:

我尝试了preg_split与以下所有组合:

  • different quotes (" and ')
  • 不同的引号(“和')
  • different escaping (\r or \r)
  • 不同的逃避(\ r或\ r)
  • different specification for character set: [\r\n], (\r|\n|\r\n|\n\r), "\r*\n"
  • 字符集的不同规范:[\ r \ n],(\ r | \ n | \ r \ n | \ n \ r \ n),“\ r * \ n”
  • modifiers to the regexp (#\r\n#ms) - and no modifier, of course
  • regexp的修饰符(#\ r \ n#ms) - 当然没有修饰符
  • in desperation, different regexp delimiters (/\r/, #\r#).
  • 无奈之下,不同的正则表达式分隔符(/ \ r /,#\ r ##)。

None worked. I can't understand why (PHP 7 on Ubuntu 16 "Xenial Xerus"). In the end I knew from hexdump that I had \n in my input (sometimes \r\n), so resorted to explode:

没有用。我无法理解为什么(Ubuntu 16“Xenial Xerus”上的PHP 7)。最后我从hexdump中知道我在输入中有\ n(有时是\ r \ n),因此使用了爆炸:

$rows = array_filter(
    explode("\n", $file['data']),
    'trim'
);

so that the \n would split and the trim would get rid of any extra \r's.

所以\ n会分裂,修剪将摆脱任何额外的\ r \ n。


I am still upvoting Brian's answer because it is correct (and the accepted answer because it worked). If anyone manages to repeat whatever stupid error I must have been doing even if I can't see what could have been going wrong in so simple a regexp and can tell it to me, there bloody will be a bounty on that.

我仍然支持Brian的答案,因为它是正确的(并且接受的答案因为它有效)。如果有人设法重复我一定要做的任何愚蠢的错误,即使我看不出在这么简单的正则表达式中可能出现什么问题而且可以告诉我,血腥将是一个赏金。