1

I have a downloaded subtitle file like below.The file contains line numbers,empty lines and timestamp. i want to remove these line number,empty line and timestamp, i need just the text only

What it is now

1
00:00:01.876 --> 00:00:02.709
<v Instructor>We can go back now</v>

2
00:00:02.709 --> 00:00:05.042
to our web server checklist.

3
00:00:06.410 --> 00:00:08.722
We've already seen better ways to organise our code

4
00:00:08.722 --> 00:00:11.545
into reusable pieces with modules,

5
00:00:11.545 --> 00:00:13.315
we've seen ways to deal with files,

6
00:00:13.315 --> 00:00:15.940
both synchronous and asynchronous,

7
00:00:15.940 --> 00:00:16.773
and buffers,

8
00:00:16.773 --> 00:00:18.325
both the built-in Node one

9
00:00:18.325 --> 00:00:20.380
and the ES6 buffers,

10
00:00:20.380 --> 00:00:22.485
and we've seen a way to deal with work

what the output should be

We can go back nowto our web server checklist.We've already seen better ways to organise our codeinto reusable pieces with modules,we've seen ways to deal with files,both synchronous and asynchronous,and buffers,both the built-in Node oneand the ES6 buffers,and we've seen a way to deal with work

Guys please help me out how to achieve this,i am using this online tool to achieve this

3
  • You didn't save your regex. You have to save it, then share the link here. And not only the link, you should actually show here what you tried, just in case the link becomes unavailable. May 15, 2018 at 11:10
  • 1
    By the way: wouldn't it be much easier to just take every fourth line, starting with the third? May 15, 2018 at 11:11
  • @AndreyTyukin thats a different way of thinking superb
    – yasarui
    May 15, 2018 at 11:19

5 Answers 5

8

You want to do the following :

  1. Select the lines beginning either by a number or a newline character.
  2. Select everything in the line until the end
  3. Also select the new-line character at the end of the line

The following regex does this exactly

^[\d\n].*\n

Demo

Little disclaimer : this will exclude the lines of text beginning with a number


Output

<v Instructor>We can go back now</v>
to our web server checklist.
We've already seen better ways to organise our code
into reusable pieces with modules,
we've seen ways to deal with files,
both synchronous and asynchronous,
and buffers,
both the built-in Node one
and the ES6 buffers,
and we've seen a way to deal with work
2

You can use more than one regexp - sequentially on results of previous regexp. Here is developed @YassinHajaj answer (regexp to remove numbers and times) to get desired output

^[\d\n].*\n               remove numbers and times                             
\<\/?v.*?\>               remove <v> tag
\R                        join multi lines into one line

Here are links to sequence of regexp used in this tool 1 -> 2 -> 3.

Output

Is exactly what you want

We can go back now to our web server checklist. We've already seen better ways to organise our code into reusable pieces with modules, we've seen ways to deal with files, both synchronous and asynchronous, and buffers, both the built-in Node one and the ES6 buffers, and we've seen a way to deal with work

1

The following will select all lines with the text (even the last line):

^[a-zA-z]*.*(\n|\Z)$
  1. [a-zA-z]* selects all lines starting with a character (zero or more times)
  2. .* any char (zero or more times)
  3. (\n|\Z) end of line or EOF

You can view it at your regex tester

1

This should be a definitive regex based on your exact needs: (?:(?:[\n$]|^\d+$)|(?:^[\n\s])|(?:\d{1,3}(?:[:.\d$]|(?:\s(?=-))))|(?:--> ))+

As an improvement over other answers, it will even work for lines starting with numbers (single digits at the moment), matching nnn:nnn formats. Try it here: https://regex101.com/r/din5tp/1/

But it is very far from perfection.

For an explaination on how it works, I recommend using the tool.

It is to be noted that any regex will ultimately have weaknesses and I don't recommend this level of complexity unless this is what you really need.

Output:

We can go back nowto our web server checklist.We've already seen better ways to organise our codeinto reusable pieces with modules,we've seen ways to deal with files,both synchronous and asynchronous,and buffers,both the built-in Node oneand the ES6 buffers,and we've seen a way to deal with work

1
  • I am glad to help
    – Attersson
    May 15, 2018 at 14:35
0
^[(\d|\n)].*$

This pattern removes every empty line or lines starting with a digit

1
  • I don't think you meant to negate your character class
    – Gary
    May 15, 2018 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.