[Tutor] Regular Expression Misunderstanding

Luke Paireepinart rabidpoobear at gmail.com
Fri Jul 14 15:10:09 CEST 2006


> I have a file full of lines beginning with the letter "b".  I want a
> RE that will return the whole line if it begins with b.
>
> I find if I do eg:
>
>   
>>>> m = re.search("^b", "b spam spam spam")
>>>> m.group()
>>>>         
> 'b'
>
> How do I get it to return the whole line if it begins with a b?
>
> S.
for line in file:
    if line.strip()[0] == 'b':
       print line

or
print [a for a in file if a.strip()[0] == b]
if you want to use list comprehension.
As for the RE way, I've no idea.


More information about the Tutor mailing list