正则表达式匹配包含特定字符的句子中的所有空格

时间:2015-10-05 11:15:12

标签: regex arguments

我正在寻找正确的正则表达式来匹配包含“ - >”的句子中的所有空格。

.*->.*\r? 

用于选择所有正确的句子,但我很难用/ s

连接它

示例文本:我尝试重新格式化字幕文本。我需要从时间码中取出所有空格:

7
00: 00: 20,509 -> 00: 00: 25,059
on the data before and does not shy in front of radical solutions such as post privacy back.

8
00: 00: 25,159 -> 00: 00: 27,896
On his blog writes sower already since 2011

9
00: 00: 27,996 -> 00: 00: 31,784
on the impact of technology on society, politics and economy.

预期结果将是:

7
00:00:20,509->00:00:25,059
on the data before and does not shy in front of radical solutions such as post privacy back.

非常感谢!

1 个答案:

答案 0 :(得分:0)

根据你的样本,正则表达式中的一些解决方案比^^!(冒号和空格)更复杂:

first one

:仅在前面有((?:(?<\d:)|(?<=->)) | (?=->))[0-9]:或紧跟->的空格

后才会选择空格

A shorter one(但更多的错误证明):

->与上面相同的想法,在前瞻中交替进行。

The most exact one与您的格式完全匹配,但需要((?:(?<=:|>)) | (?=->))而不是替换

\1\2\3-->\4\5\6