0

I want a function to find a pattern(word character) on each line inside a file. my code is sort of working but it does not go further after reading the first line. can anyone help? import re

    inputtext = open('input.txt', 'r+')
    inputtext1 = inputtext.read()


    match = re.search(r'([matchwordinline].*\n)+', inputtext1)



    if match:

            match1 = match.group()
    print match1

1 Answer 1

1

re.search matches only one instance.. Try re.findall

list_name = re.findall(r'([matchwordinline].*\n)+', inputtext1)

For more Visit https://docs.python.org/2/library/re.html?highlight=matching%20searching#finding-all-adverbs

import re
inputtext = open('input.txt', 'r+')
inputtext1 = inputtext.read()

match = re.findall(r'([your word].*\n)+', inputtext1)
print match

This is my input.txt

cat
alicecat deaf
cut cat crazy
buttercup ruin
youseeacatidont

When I search for word cat i got the following output

['cat\n', 'cat deaf\n', 'cat crazy\n', 'catidont\n']

Hope this is what you meant..

Sign up to request clarification or add additional context in comments.

3 Comments

your suggestion did not help. re.findall will find only the matching word. I want a given pattern following the rest of the line.
You said you want a given pattern following the rest of the line.
@user3395021 The rest of the line is available in it. Check again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.