-3

I have a huge text document lets say of 1000's of words and with way too many lines in the text.

I want to grab specific value of a particular word. In my example word is Teetotlar and value is Yes or No

Sample text:

I am going to a party with my friends. I have lot of friends. they are all coming to party. I will be happy to see each of them.

Many of my friends drink but if you ask them r you teetotlar, they would probably reply yes.

But it is all good.

I have working code for:

  1. Read all lines one by one

  2. Check for teetotlar word

  3. Again check if that have "Yes" or "No" and then save that value in one variable

What I want:

Is to have a regex which just checks for the word Teetotlar in the whole text and then looks for value Yes or No in the same line.

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51

1 Answers1

2

Here is one approach:

line = "Many of my friends drink but if you ask them r you teetotlar, they would probably reply yes."

m = re.search( r'\bteetotlar\b.*\b(?:yes|no)\b', line, re.M|re.I|re.DOTALL)
if m:
    print "the line matches"

This would let you check the line for teetotler and yes/no, with the former occurring before the latter.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360