1

I am wondering how to implement Aaron Digulla's answer in this question: Fastest Text search method in a large text file

with open ('test.txt', 'rt') as myfile:
    contents = myfile.read() 
    match = re.search("abc", contents)

What's next, so that I can find the previous EOL and next EOL, and so that I can extract the line?

desmond ng
  • 89
  • 4

2 Answers2

0

You can use the start index of the match object to find the previous and next EOL using str.find and str.rfind with their start and end parameters:

with open ('test.txt', 'rt') as myfile:
    contents = myfile.read() 
    match = re.search("abc", contents)
    start = match.start()
    previous_EOL = contents.rfind('\n', 0, start)
    next_EOL = contents.find('\n', start)
    line = contents[previous_EOL+1: next_EOL]

For example:

contents = '''
This is a sample text
Here is 'abc' in this line.
There are some other lines.'''

match = re.search("abc", contents)
start = match.start()
previous_EOL = contents.rfind('\n', 0, start)
next_EOL = contents.find('\n', start)
line = contents[previous_EOL+1: next_EOL]

print(line)

Prints:

Here is 'abc' in this line.
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
0

Replace

match = re.search("abc", contents)

with

match = re.search("^.*abc.*$", contents, re.M)

It will match the whole line containing "abc". Used with re.M flag ^ matches the beginning of a line and $ its end.

Here is an example:

import re

s = """
Twinkle, twinkle
Little star!
How I wonder 
What you are!
"""

term = "star"
match = re.search(f"^.*{term}.*$", s, re.M)
print(match.group(0))

It gives:

Little star!
bb1
  • 7,174
  • 2
  • 8
  • 23
  • May I also know what to do if I want the last matching line including the substring? Is re.findall() the best way? For example, I want "What you are!" but not "Little star!", when I search the character "!". – desmond ng Feb 21 '21 at 12:41
  • Either re.findall() or you can try to use [regex](https://pypi.org/project/regex/) module which allows searching a string in the reversed direction. – bb1 Feb 21 '21 at 14:53