Perl 如何拆分字符串并在日期之前添加新行

Perl 如何拆分字符串并在日期之前添加新行,perl,Perl,如何在日期之前拆分字符串并添加新行 例如 $string = 4/25/2011 11:34:07 AM [test] >> Need to change. 5/16/2011 10:44:45 AM [test] >> Nothing to change yet. Need to review information. 5/23/2011 11:13:39 AM [test] >> Working on it. 预期结果: 4/25/2011 11:3

如何在日期之前拆分字符串并添加新行

例如

$string = 4/25/2011 11:34:07 AM [test] >> Need to change.  5/16/2011 10:44:45 AM [test] >> Nothing to change yet. Need to review information.  5/23/2011 11:13:39 AM [test] >> Working on it.
预期结果:

4/25/2011 11:34:07 AM [test] >> Need to change.  
5/16/2011 10:44:45 AM [test] >> Nothing to change yet. Need to review information.  
5/23/2011 11:13:39 AM [test] >> Working on it.
我试过了

my @words1 = split /\d{2}\\\d{2}\\(?:\d{2}|\d{4})\s+\d{2}:\d{2}/, $str;
但没有返回所需的输出

在没有亲自尝试解决问题的情况下,只要求堆栈溢出不是很好的形式。如果没有任何代码,就没有什么可以帮助的,我希望您将来至少会做出努力

假设在每个日期之前总是有一些空格,这对你来说是可行的

严格使用; 使用“全部”警告; 使用特征“说”; my$string='4/25/2011 11:34:07 AM[test]>>需要更改。2011年5月16日上午10:44:45[测试]>>没有任何变化。需要审查信息。2011年5月23日上午11:13:39[测试]>>正在进行测试。”; $string=~s |\s+?=\d{1,2}/\d{1,2}/\d{4}\n | g; 说$string; 输出 2011年4月25日上午11:34:07[测试]>>需要更改。 2011年5月16日上午10:44:45[测试]>>没有任何变化。需要审查信息。 2011年5月23日上午11:13:39[测试]>>正在进行测试。 试试这个:

my $string = "4/25/2011 11:34:07 AM [test] >> Need to change.  5/16/2011 10:44:45 AM [test] >> Nothing to change yet. Need to review information.  5/23/2011 11:13:39 AM [test] >> Working on it.";

$string=~s/\b(\d{1,2}\/\d{1,2}\/\d{4})/\n$1/igs;

$string=~s/^\s*//igs;

print"$string\n";`

通过编写代码。现在尝试使用点拆分,但我也尝试使用正则表达式@words1=split/\./,$str;编辑您的问题以添加您尝试过的代码;但它并没有返回所需的输出。@VimalRaj:这不是我写的感谢您的帮助@Borodin。我已经提到了我之前尝试过的东西。您的代码块工作正常。在这种情况下不需要使用is标志。