On 3/10/06, tom arnall <[EMAIL PROTECTED]> wrote:
> the following script is intended to uppercase every character preceded by [?.]
> and one or two spaces:
>
> ($f,$g)=/([?.] {1,2})([a-z])/s;
> $h = uc $g;
> s/$f$g/$f$h/s;
>
> in the third line however perl interprets the [.?] in $f as a metacharacter.
> is there any way to get perl to interpret the contents of $f as a literal? or
> is there another way entirely to accomplish the same goal?
>
>
> thanks,
>
> tom arnall
> north spit, ca
I believe you problem is that you are using a variable in a regex with
out the proper escape sequences (\Q\E). Try
s/\Q$f\E$g/$f$h/s;
This seems to be the hard way though. You can use another escape
sequence (\U) to uppercase a string. This lets you get away with only
one substitution.
#!/usr/bin/perl
use strict;
my $string = "this is a normal sentence.
IS THIS ANOTHER NORMAL SENTENCE? IT Is.";
print "$string\n\n";
#with documentation
my $correct = lc $string;
$correct =~ s{
( #match the beginning of a sentence
\A #match the start of the string
\s* #followed by optional white space
| #or
[.?!] #match '.', '?', or '!'
\s+ #followed by one or more spaces
)
( #match the first character of the next sentence
.
)
}{
"$1" . #keep the end of the sentence (or start of string)
"\u$2\E" #uppercase the first letter of the next senetence
}gsex;
print "$correct\n\n";
#short way
$string = lc $string;
$string =~ s/(\A\s*|[.?!]\s+)(.)/$1\u$2\E/gs;
print "$string\n";
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>