[Tutor] regex question

Richard D. Moores rdmoores at gmail.com
Tue Jan 4 17:07:53 CET 2011


On Tue, Jan 4, 2011 at 07:55, Wayne Werner <waynejwerner at gmail.com> wrote:
> On Tue, Jan 4, 2011 at 9:37 AM, Richard D. Moores <rdmoores at gmail.com>

> You could use (2.6+ I think):
> word = raw_input('Enter word to search for: ')
> with open('somefile.txt') as f:
>    for line in f:
>        if word in line:
>             print line

I think I do need a regex for cases such as this:

A file has these 2 lines:

alksdhjf ksjhdf kjshf dex akjdhf jkdshf jsdhf
alkdshf jkashd flkjdsf index alkdjshf alkdjshf

And I want the only line that contains the word "dex"

Dick


More information about the Tutor mailing list