-2

In the below string i have to remove the lines that start with a date and end with (work notes).

input_String= "22/01/2020 - aman singh (Work notes)
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood

21/01/2020 - tom cruise (Work notes)
And looked down one as far as I could
To where it bent in the undergrowth.

23/01/2020 - tom cruse (Work notes)
Then took the other, as just as fair
And having perhaps the better claim."

output_string=

Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood

And looked down one as far as I could
To where it bent in the undergrowth.

Then took the other, as just as fair
And having perhaps the better claim.
  • 1
    What have you tried? What didn't work? What did you get? What did you expect? What doesn't work with your code and where is it? – Toto Jan 30 '20 at 19:32
  • This is a fairly easy question, but I'd like to know what you've tried and where you're stumbling before providing an answer. – Zaelin Goodman Jan 30 '20 at 19:33

2 Answers2

0

You can use a multiline regular expression with sub to replace the lines.

import re 

pat = re.compile(r'^\d{2}/\d{2}\/\d{4}.+\(Work notes\)\n', flags=re.M)
# Match from date pattern at start of line to (Work notes) at the end

print(pat.sub('', input_String))  # replace that line with empty string
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0
import re

input_String= '''22/01/2020 - aman singh (Work notes)
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood

21/01/2020 - tom cruise (Work notes)
And looked down one as far as I could
To where it bent in the undergrowth.

23/01/2020 - tom cruse (Work notes)
Then took the other, as just as fair
And having perhaps the better claim.'''


temp = re.compile(r'\d{2}/\d{2}/\d{4} - [a-zA-Z ]+ \(Work notes\)\n') # saving template of regex
string = temp.sub('', input_String)  # replacing regex template in string to ''
print(string)
n1tr0xs
  • 408
  • 2
  • 9