[Tutor] re and MULTILINE

Marilyn Davis marilyn at deliberate.com
Wed Feb 21 04:02:12 CET 2007


On Tue, 20 Feb 2007, Kent Johnson wrote:

> Marilyn Davis wrote:
> > Hello Tutors,
> >
> > I'm trying to get a grip on MULTILINE and I guess I don't have it.
> >
> > Here's some code:
> >
> > #!/usr/bin/env python
> > import re
> >
> > def sub_it(mo):
> >     return 'xxx'
> >
> > def test(re_str, data):
> >     return re.sub(re_str, sub_it, data, re.MULTILINE)
> >   
> The fourth argument to re.sub() is a count, not flags. You have to 
> compile the regex and pass the MULTILINE flag to the compile, or include 
> the flag in the actual regex with *|(?m)|*.

Thank you so much!!  Duh.  Geeso, I looked at that for hours.

I should have asked for help sooner.

Gratefully,

Marilyn

> 
> Kent
> 
> 
> >         
> > if __name__ == '__main__':
> >     data = '''Betty Boop:245-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
> > Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
> > James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
> > '''
> >     re_str = r'''(d+)$'''
> >     print test(re_str, data)    
> >     
> >     re_str = r'''(d+)'''
> >     print test(re_str, data)
> >
> > '''
> > ./re_test2.py
> > Betty Boop:245-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
> > Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
> > James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:xxx
> >
> > Betty Boop:xxx-xxx-xxx:xxx Ware Road, Milton, PA xxx:xxx/xxx/xxx:43500
> > Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
> > James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
> > '''
> >
> > If I don't anchor it with '$', it gets all the digit-groups in the first line except the last one.  Why not the last one?  Whay not the other lines?
> >
> > If I do anchor it, it only gets the last group on the last line.  What's up with that?
> >
> > What I really want, is to mess with each of the last digit-groups on each line.  But I can't find them.
> >
> > The exercise is from Ellie Quigley's "Perl by Example"
> >
> > Thank you for any help.
> >
> > Marilyn Davis
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >   
> 
> 

-- 



More information about the Tutor mailing list