Php Regex-不跟时间的新行
我需要为每一条空行执行一次预更换,该空行后面没有以下内容:Php Regex-不跟时间的新行,php,regex,preg-replace,Php,Regex,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
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
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
文件所需输出:
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
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
我的职能:
$content = preg_replace("/regex expresion/", "", $file_content);
编辑2:
刚刚发现我需要找到另一种格式:
选择2
文件输入:
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
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
文件所需输出:
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
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:
$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}))/";
或者可能是你的情况
\r\n(\D|!(^\d{2}:\d{2}:\d{2}\.\d{3}\s-->\s\d{2}:\d{2}:\d{2}\.\d{3}))
更新PHP
这应该行得通
$pattern=\n{2,}(?=\D|(^([0-9]{1,3}[:.]?){4})([\s->]+([0-9]{1,3}[:.]?){4}))
preg\u replace($pattern,“,$string)
说明:
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/', '$1', $str);
您可以提供原始数据样本和预期结果吗?尝试
([0-9]{1,3}[:.]??){4})([\s->]+)([0-9]{1,3}[:.]??){4}
稍微偏离主题,但使用类似的方法匹配时间字符串将避免大量的错误repition@Alex非常感谢。这是我找到的最接近解决方案。唯一的问题是它会替换除时间行之前的一行之外的所有新行。@Alex我已经添加了一个示例输入和所需的输出。谢谢,嘿,Tnx。它看起来应该可以工作,但在php preg_替换中它什么也不做。My func:$file_content=preg_replace(“/\r\n(\D|!(^\D{2}:\D{2}:\D{2}\.\D{3}\s-->\s\D{2}:\D{2}:\D{2}\.\D{3}))/”,“$$$$$”,$file\u content);嘿,它在一行上工作,对于一个更复杂的文本,它在我把(\R){2,}叮当到(\R){1,}之后工作。我还需要(抱歉今天发现)对这样的行执行相同的更改:5\n 00:00:02.800-->00:00:04.800\n我编辑了我的问题并添加了我的意思。非常感谢你-我真的需要提高我的正则表达式技能。如果一些行以数字开头,它会起作用吗。取而代之的是“第1行”将出现“第1行…”?@matisa:它删除“时间”之前的换行符或不包含数字的行。不幸的是,我不能排除这些行将以数字开头,因为这些是字幕。我不知道这行将包含什么,但我知道一行数字后面跟时间行将始终是一个新的太阳指示器。