[Tutor] re and MULTILINE

Marilyn Davis marilyn at deliberate.com
Wed Feb 21 00:05:04 CET 2007


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)
        
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




More information about the Tutor mailing list