X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq6.pod;h=4e5ba50ee5167b880fa638aaaa732d1b3898c98e;hb=83272a45226e83bd136d713158e9b44ace2dbc8d;hp=a032d49a703569b56cb684f65ce6b7dd1a97562e;hpb=a6dd486b7feb5918da837e5ad585c8ce954f9bbf;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod index a032d49..4e5ba50 100644 --- a/pod/perlfaq6.pod +++ b/pod/perlfaq6.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq6 - Regexes ($Revision: 1.27 $, $Date: 1999/05/23 16:08:30 $) +perlfaq6 - Regular Expressions ($Revision: 1.12 $, $Date: 2002/06/01 22:31:09 $) =head1 DESCRIPTION @@ -9,7 +9,7 @@ littered with answers involving regular expressions. For example, decoding a URL and checking whether something is a number are handled with regular expressions, but those answers are found elsewhere in this document (in L: ``How do I decode or create those %-encodings -on the web'' and L: ``How do I determine whether a scalar is +on the web'' and L: ``How do I determine whether a scalar is a number/whole/integer/float'', to be precise). =head2 How can I hope to use regular expressions without creating illegible and unmaintainable code? @@ -70,9 +70,9 @@ delimiter within the pattern: =head2 I'm having trouble matching over more than one line. What's wrong? -Either you don't have more than one line in the string you're looking at -(probably), or else you aren't using the correct modifier(s) on your -pattern (possibly). +Either you don't have more than one line in the string you're looking +at (probably), or else you aren't using the correct modifier(s) on +your pattern (possibly). There are many ways to get multiline data into a string. If you want it to happen automatically while reading input, you'll want to set $/ @@ -115,7 +115,7 @@ Here's code that finds everything between START and END in a paragraph: undef $/; # read in whole file, not just one line or paragraph while ( <> ) { - while ( /START(.*?)END/sm ) { # /s makes . cross line boundaries + while ( /START(.*?)END/sgm ) { # /s makes . cross line boundaries print "$1\n"; } } @@ -166,7 +166,7 @@ appear within a certain time. close FH; ## Get a read/write filehandle to it. - $fh = new FileHandle "+ and its ilk), they still aren't powerful enough--with -the possible exception of bizarre and experimental features in the -development-track releases of Perl. You still need to use non-regex -techniques to parse balanced text, such as the text enclosed between -matching parentheses or braces, for example. +Historically, Perl regular expressions were not capable of matching +balanced text. As of more recent versions of perl including 5.6.1 +experimental features have been added that make it possible to do this. +Look at the documentation for the (??{ }) construct in recent perlre manual +pages to see an example of matching balanced parentheses. Be sure to take +special notice of the warnings present in the manual before making use +of this feature. + +CPAN contains many modules that can be useful for matching text +depending on the context. Damian Conway provides some useful +patterns in Regexp::Common. The module Text::Balanced provides a +general solution to this problem. + +One of the common applications of balanced text matching is working +with XML and HTML. There are many modules available that support +these needs. Two examples are HTML::Parser and XML::Parser. There +are many others. An elaborate subroutine (for 7-bit ASCII only) to pull out balanced and possibly nested single chars, like C<`> and C<'>, C<{> and C<}>, or C<(> and C<)> can be found in -http://www.perl.com/CPAN/authors/id/TOMC/scripts/pull_quotes.gz . +http://www.cpan.org/authors/id/TOMC/scripts/pull_quotes.gz . -The C::Scan module from CPAN contains such subs for internal use, +The C::Scan module from CPAN also contains such subs for internal use, but they are undocumented. =head2 What does it mean that regexes are greedy? How can I get around it? @@ -404,7 +429,7 @@ expression engine to find a match as quickly as possible and pass control on to whatever is next in line, like you would if you were playing hot potato. -=head2 How do I process each word on each line? +=head2 How do I process each word on each line? Use the split function: @@ -609,18 +634,29 @@ L). =head2 What's wrong with using grep or map in a void context? -Both grep and map build a return list, regardless of their context. -This means you're making Perl go to the trouble of building up a -return list that you then just ignore. That's no way to treat a -programming language, you insensitive scoundrel! +The problem is that both grep and map build a return list, +regardless of the context. This means you're making Perl go +to the trouble of building a list that you then just throw away. +If the list is large, you waste both time and space. If your +intent is to iterate over the list then use a for loop for this +purpose. =head2 How can I match strings with multibyte characters? -This is hard, and there's no good way. Perl does not directly support -wide characters. It pretends that a byte and a character are -synonymous. The following set of approaches was offered by Jeffrey -Friedl, whose article in issue #5 of The Perl Journal talks about this -very matter. +Starting from Perl 5.6 Perl has had some level of multibyte character +support. Perl 5.8 or later is recommended. Supported multibyte +character repertoires include Unicode, and legacy encodings +through the Encode module. See L, L, +and L. + +If you are stuck with older Perls, you can do Unicode with the +C module, and character conversions using the +C and C modules. If you are using +Japanese encodings, you might try using the jperl 5.005_03. + +Finally, the following set of approaches was offered by Jeffrey +Friedl, whose article in issue #5 of The Perl Journal talks about +this very matter. Let's suppose you have some weird Martian encoding where pairs of ASCII uppercase letters encode single Martian letters (i.e. the two @@ -694,15 +730,11 @@ in L. =head1 AUTHOR AND COPYRIGHT -Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington. +Copyright (c) 1997-2002 Tom Christiansen and Nathan Torkington. All rights reserved. -When included as part of the Standard Version of Perl, or as part of -its complete documentation whether printed or otherwise, this work -may be distributed only under the terms of Perl's Artistic License. -Any distribution of this file or derivatives thereof I -of that package require that special arrangements be made with -copyright holder. +This documentation is free; you can redistribute it and/or modify it +under the same terms as Perl itself. Irrespective of its distribution, all code examples in this file are hereby placed into the public domain. You are permitted and