Perl Range operator paradigm

Peter Hansen peter at engcorp.com
Mon Oct 22 13:45:02 CEST 2001


Laurent Pierron wrote:
> 
> Hello Gurus,
> 
> In Perl there is a very powerful range operator '..', which, in scalar
> context, is acting like line-range (,) operator in sed and awk.
> 
> By example, this small program prints all the lines in the file included
> between <BODY>...</BODY> :
> 
> while (<>) {
>    if (/<BODY>/ .. /<\/BODY>/) {print; }
> }
> 
> How can I do, the same thing in Python ?

Requirements insufficiently detailed.  What happens, for example,
if there are multiple <BODY> </BODY> pairs in the file?  Does
it do a "greedy" match and use the first <BODY> and the last
</BODY>, or does it do each pair separately?

Also, is it case-sensitive?  HTML is not, but XML (and thus
XHTML) is.  Which does the above do?

If there were text on the line after <BODY>, would it print
that text or, as you imply, only the *lines* between the two
tags?  What if both appeared on the same line?  Any output?

(I think it must already be written somewhere as a law
that even the specifications for a Perl one-liner cannot
easily be described ... :-)

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list