[Tutor] Searching list items.
John Fouhy
john at fouhy.net
Wed Oct 18 00:01:56 CEST 2006
On 18/10/06, Chris Hengge <pyro9219 at gmail.com> wrote:
> I remove those lines, but I was trying to use
> for line in contents:
> result = re.search("something", line)
> print result
>
> this printed out something like
>
> None
> None
> None
> hex memory address of goodness
> None
> None
If you don't need a regular expression, you are probably better off using 'in':
for line in contents:
if 'something' in line:
print line
Otherwise, re.search() and re.match() return either None or a match
object. So, you can do this:
for line in contents:
m = re.search('something', line)
if m:
print line[m.start():m.end()]
match objects become more powerful when you start using groups or
named groups in your regular expressions. See the documentation for
more :-)
--
John.
More information about the Tutor
mailing list