0

I have a text file containing data as given below. I have to extract all lines containing signed

The document was signed on July 12

The document was signed by Charlie

This document was assigned to John

The document was preassigned to Amanda

Expected output:

The document was signed on July 12

The document was signed by Charlie

If I am using:

for line in file:
    if "signed" in line:
        print (line)

It is printing all the lines

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Slickmind
  • 442
  • 1
  • 7
  • 15

1 Answers1

0

This is easily done by using a word boundary \b. \bsigned will match signed, but not assigned.

See here

You can use re.search(line, ".*\bsigned.*")

emsimpson92
  • 1,779
  • 1
  • 9
  • 24
  • Thank you! I am new to regex. I studied about word boundary but it did not strike to my mind. Thank you again! – Slickmind Jul 16 '18 at 22:26
  • yes it is similar to the duplicate link which you posted but I would not have solved it by referring that example as I am new regex – Slickmind Jul 17 '18 at 00:02