Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the grep equivalent in Python?

Tags:

python

regex

Say I have a text file with the line 'I like elephants'. If I cat the said file and the pipe it to 'grep elephants', I get the entire line "I like elephants".

How do I achieve this functionality in Python with re? Ive been trying the following:

test = re.search('elephants', 'I like elephants.\nThey are nice')
test.group(0)

I get only the word 'elephants' and not the whole sentence as the output.

How do I get the entire sentence? Thank you.

like image 782
Zohair Avatar asked Dec 26 '17 10:12

Zohair


People also ask

What is grep in Python?

GREP in Python Created: March-20, 2021 GREP is an interesting command-line feature that allows us to search plain text files for specific lines using regular expressions. Regular expressions are very heavily used in Python and can be used to check if a string matches a pattern or not.

How do I Grep a regex in Python?

A regex in Python, either the search or match methods, returns a Match object or None. For grep -v equivalent, you might use: import re for line in sys.stdin: if re.search (r' [a-z]', line) is None: sys.stdout.write (line)

How to implement grep in Python with Glob?

The glob module allows us to find the paths of files that match a pattern in a directory. Its use in replicating GREP in Python can be seen below. The iglob () function creates an object that returns the files in the directory, which is passed to the function. Another concise way of implementing GREP in just a few lines is shown below.

How to grep-V [A-Z] in Python?

A regex in Python, either the search or match methods, returns a Match object or None. For grep -v equivalent, you might use: Show activity on this post. It turns out you can just use [^a-z] to mean grep -v [a-z].


1 Answers

You could use the in keyword to check for your substring:

with open('text_file.txt', 'r') as f:
    for line in f.readlines():
        if 'elephant' in line:
            print(line)

Or, if you had a string s with \n characters:

for line in s.split('\n'):
    if 'elephant' in line:
        print(line)

Your regex only prints elephant because that's what it captured: exactly your regex string. If you were to try the following regex instead:

test = re.search(r'(.*?elephants.*?)\n', 'I like elephants.\nThey are nice')

Then you'd have results for test.group(0) and test.group(1) which include the whole line before and after the elephants.

In [22]: test.group(0)
Out[22]: 'I like elephants.\n'

That's the whole captured string.

In [23]: test.group(1)
Out[23]: 'I like elephants.'

That's just the capture group (string between parentheses).

like image 119
Pedro von Hertwig Batista Avatar answered Oct 11 '22 02:10

Pedro von Hertwig Batista