This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Rename perlunintro to perluniintro; regen toc.
[perl5.git] / pod / perlfaq6.pod
index ed6c01b..be7e8ec 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq6 - Regexes ($Revision: 1.27 $, $Date: 1999/05/23 16:08:30 $)
+perlfaq6 - Regexes ($Revision: 1.4 $, $Date: 2001/11/09 08:06:04 $)
 
 =head1 DESCRIPTION
 
@@ -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";
        }
     }
@@ -194,7 +194,7 @@ properties of bitwise xor on ASCII strings.
 
     print;
 
-And here it is as a subroutine, modelled after the above:
+And here it is as a subroutine, modeled after the above:
 
     sub preserve_case($$) {
        my ($old, $new) = @_;
@@ -212,6 +212,21 @@ This prints:
 
     this is a SUcCESS case
 
+As an alternative, to keep the case of the replacement word if it is
+longer than the original, you can use this code, by Jeff Pinyan:
+
+  sub preserve_case {
+    my ($from, $to) = @_;
+    my ($lf, $lt) = map length, @_;
+    
+    if ($lt < $lf) { $from = substr $from, 0, $lt }
+    else { $from .= substr $to, $lf }
+    
+    return uc $to | ($from ^ uc $from);
+  }
+
+This changes the sentence to "this is a SUcCess case."
+
 Just to show that C programmers can write C in any programming language,
 if you prefer a more C-like solution, the following script makes the
 substitution have the same case, letter by letter, as the original.
@@ -368,20 +383,30 @@ A slight modification also removes C++ comments:
 
 =head2 Can I use Perl regular expressions to match balanced text?
 
-Although Perl regular expressions are more powerful than "mathematical"
-regular expressions because they feature conveniences like backreferences
-(C<\1> 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?
@@ -697,12 +722,8 @@ in L<perlre>.
 Copyright (c) 1997-1999 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<outside>
-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