Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 6.6 How do I substitute case insensitively on the LHS while preserving case on the RHS?

Reply
Thread Tools

FAQ 6.6 How do I substitute case insensitively on the LHS while preserving case on the RHS?

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      02-08-2011
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

6.6: How do I substitute case insensitively on the LHS while preserving case on the RHS?

Here's a lovely Perlish solution by Larry Rosler. It exploits properties
of bitwise xor on ASCII strings.

$_= "this is a TEsT case";

$old = 'test';
$new = 'success';

s{(\Q$old\E)}
{ uc $new | (uc $1 ^ $1) .
(uc(substr $1, -1) ^ substr $1, -1) x
(length($new) - length $1)
}egi;

print;

And here it is as a subroutine, modeled after the above:

sub preserve_case($$) {
my ($old, $new) = @_;
my $mask = uc $old ^ $old;

uc $new | $mask .
substr($mask, -1) x (length($new) - length($old))
}

$string = "this is a TEsT case";
$string =~ s/(test)/preserve_case($1, "success")/egi;
print "$string\n";

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. (It
also happens to run about 240% slower than the Perlish solution runs.)
If the substitution has more characters than the string being
substituted, the case of the last character is used for the rest of the
substitution.

# Original by Nathan Torkington, massaged by Jeffrey Friedl
#
sub preserve_case($$)
{
my ($old, $new) = @_;
my ($state) = 0; # 0 = no change; 1 = lc; 2 = uc
my ($i, $oldlen, $newlen, $c) = (0, length($old), length($new));
my ($len) = $oldlen < $newlen ? $oldlen : $newlen;

for ($i = 0; $i < $len; $i++) {
if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) {
$state = 0;
} elsif (lc $c eq $c) {
substr($new, $i, 1) = lc(substr($new, $i, 1));
$state = 1;
} else {
substr($new, $i, 1) = uc(substr($new, $i, 1));
$state = 2;
}
}
# finish up with any remaining new (for when new is longer than old)
if ($newlen > $oldlen) {
if ($state == 1) {
substr($new, $oldlen) = lc(substr($new, $oldlen));
} elsif ($state == 2) {
substr($new, $oldlen) = uc(substr($new, $oldlen));
}
}
return $new;
}



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: How to check a String contains another String,case-insensitively? tomaszewski.p Java 4 09-12-2008 05:40 PM
Re: How to check a String contains another String, case-insensitively? Roedy Green Java 0 09-12-2008 04:26 PM
Re: How to check a String contains another String, case-insensitively? Knute Johnson Java 0 09-11-2008 07:59 PM
Sorting case insensitively John Deuf Perl Misc 4 10-31-2003 02:45 PM



Advertisments