Getting a line in a large file's content

Viewed 93

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?

2 Answers

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.

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!
Related