Python equivalent of Perl's $/

Nick Craig-Wood nick at craig-wood.com
Mon Aug 20 11:30:10 EDT 2007


John K Masters <johnmasters at oxtedonline.net> wrote:
>  I am currently working my way through Jeffrey Friedl's book Mastering
>  Regular Expressions. Great book apart from the fact it uses Perl for the
>  examples.
> 
>  One particular expression that interests me is '$/ = ".\n"' which,
>  rather than splitting a file into lines, splits on a period-newline
>  boundary. Combined with Perl's 'while (<>)' construct this seems a great
>  way to process the files I am interested in.
> 
>  Without wishing to start a flame war, is there a way to do this in Python?
> 
>  Regards, John

Something like this maybe?

import re

input_data = """I am currently working my way through Jeffrey Friedl's book Mastering
Regular Expressions. Great book apart from the fact it uses Perl for the
examples.

One particular expression that interests me is '$/ = ".\\n"' which,
rather than splitting a file into lines, splits on a period-newline
boundary. Combined with Perl's 'while (<>)' construct this seems a great
way to process the files I am interested in.

Without wishing to start a flame war, is there a way to do this in Python?
"""

for para in re.split(r"\.\n", input_data):
    print "para = %r" % para

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list