正则表达式 - 后面没有时间的新行

Regex - new line that is not followed by time

我需要为每个没有跟随此的空行预先形成一个 preg_replace:

00:00:02.800 --> 00:00:04.800

其格式为:

any 2 digits:any 2 digits:any 2 digits.any 3 digits --> any 2 digits:any 2 digits:any 2 digits.any 3 digits

我知道如何搜索空行:

"/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/"

时间行:

[0-9]{1,2}[:.,-]?[:][0-9]{1,2}[:.,-]?[:][0-9]{1,2}[:.,-]?[.][0-9]{1,3}[:.,-]?[\s][-][-][>][\s][0-9]{1,2}[:.,-]?[:][0-9]{1,2}[:.,-]?[:][0-9]{1,2}[:.,-]?[.][0-9]{1,3}[:.,-]?

但是我无法创建一个正则表达式来仅查找时间行后面没有的行。

编辑: 选项1 文件输入:

WEBVTT

00:00:00.300 --> 00:00:01.000
line1
  
line2
line3

00:00:01.000 --> 00:00:02.800
line1

00:00:02.800 --> 00:00:04.800
line1
line2


line3

文件所需的输出:

WEBVTT

00:00:00.300 --> 00:00:01.000
line1  
line2
line3

00:00:01.000 --> 00:00:02.800
line1

00:00:02.800 --> 00:00:04.800
line1
line2
line3

我的函数:

 $content = preg_replace("/regex expresion/", "", $file_content);

编辑 2:

刚发现我需要找到另一种格式: 选项 2 文件输入:

1
00:00:00,300 --> 00:00:01,000
line 1 line 1

line 2

2
00:00:01,000 --> 00:00:02,800
line 1 line 1 line 1

line2


line 3 line 3

3
00:00:02,800 --> 00:00:04,800
line 1

文件所需的输出:

1
00:00:00,300 --> 00:00:01,000
line 1 line 1
line 2

2
00:00:01,000 --> 00:00:02,800
line 1 line 1 line 1
line2
line 3 line 3

3
00:00:02,800 --> 00:00:04,800
line 1

Totos 的回答很有效。我试图根据我的需要修改它,但没有成功。 我试过了:

/(\R){1,}(?!(\d\R\d\d:\d\d:\d\d\.\d{3}) --> (?2))

已解决

解决方案:

选项 1:

$regex = "/(\R){1,}(?=(\d\d:\d\d:\d\d\.\d{3}) --> (?2))/";

选项 2:

$regex = "/(\R)(?!(\d\R\d\d:\d\d:\d\d\,\d{3}))/";

https://regex101.com/r/feqs76/3/

\n\n(\D|!(^\d{2}:\d{2}:\d{2}\.\d{3}\s-->\s\d{2}:\d{2}:\d{2}\.\d{3}))

或者可能是你的情况

\r\n(\D|!(^\d{2}:\d{2}:\d{2}\.\d{3}\s-->\s\d{2}:\d{2}:\d{2}\.\d{3}))

更新 PHP

https://ideone.com/hRYizH

$regexp = '/(\R)(\R*)(\D|!(\d{2}:\d{2}:\d{2}\.\d{3}\s-->\s\d{2}:\d{2}:\d{2}\.\d{3}))/';
$content = preg_replace($regexp, "\n", $file_content);

这应该有效

$pattern = \n{2,}(?=\D|(^(([0-9]{1,3}[:.]??){4})([\s->]+)(([0-9]{1,3}[:.]??){4})))

Example Here

preg_replace($pattern, "", $string)

$str = <<<EOD
1
00:00:00,300 --> 00:00:01,000
line 1 line 1

line 2

2
00:00:01,000 --> 00:00:02,800
line 1 line 1 line 1

line2


line 3 line 3

3
00:00:02,800 --> 00:00:04,800
line 1
EOD;

$str =preg_replace('/(\R)+(?!\d)/', '', $str);
echo $str,"\n";

给定示例的输出:

00:00:00,300 --> 00:00:01,000
line 1 line 1
line 2

2
00:00:01,000 --> 00:00:02,800
line 1 line 1 line 1
line2
line 3 line 3

3
00:00:02,800 --> 00:00:04,800
line 1

解释:

(\R)+       : group 1, any kind of linebreak, 2 or more times
(?!\d)      : negative lookahead, make sure we don't have digit after

或者,如果 lineX 可以以数字开头:

$str =preg_replace('/(\R){2,}(?!(\d\d:\d\d:\d\d\.\d{3}) --> (?2)|\d+)\R/', '', $str);