=head1 NAME
-perlfaq6 - Regexes ($Revision: 1.27 $, $Date: 1999/05/23 16:08:30 $)
+perlfaq6 - Regexes ($Revision: 1.1 $, $Date: 2001/09/20 03:03:00 $)
=head1 DESCRIPTION
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";
}
}
$old = 'test';
$new = 'success';
- s{(\Q$old\E}
+ s{(\Q$old\E)}
{ uc $new | (uc $1 ^ $1) .
(uc(substr $1, -1) ^ substr $1, -1) x
(length($new) - length $1)
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.
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:
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