in reply to Case-sensitive substitution with case-insensitive matches
#!/usr/local/bin/perl -l -w use strict; my %repl_words = (sadboy=>'badboy', sadgirl=>'badgirl'); my $re = join('|', keys %repl_words); $re = qr/$re/i; my $str = "this SadBoy and SadGirl are..."; $str =~ s/($re)/fix_case($1, $repl_words{lc($1)})/eg; print $str; sub fix_case { my $match_word = shift; my $replace_word = shift; my $i = 0; for (split '', $match_word) { next if $_ eq lc; substr($replace_word, $i, 1) = uc substr($replace_word, $i, 1); } continue { $i++ } return $replace_word; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Case-sensitive substitution with case-insensitive matches
by snax (Hermit) on Dec 02, 2000 at 20:56 UTC |