in reply to regular expression search and replace
You seem to be parsing out sentences, and that's a tricky, natural language sort of thing to do, and perhaps not best done with regexen. If you can do it, the \u \U \l \L "interpolation control" (as I think of them) operators or escape sequences may come in handy for the transformations you want (see Quote and Quote-like Operators in perlop — and don't forget \Q \E too). You can avoid the /e regex modifier. With a very naive definition of a sentence pattern:
c:\@Work\Perl>perl -wMstrict -le "my $s = 'perlmonks IS a gREat site. dO you tHiNk SO? I loVE it!'; print qq{'$s'}; ;; my $senterm = '.?!'; my $sentence = qr{ \b [[:alpha:]] [^\Q$senterm\E]* [\Q$senterm\E] }xm +s; ;; $s =~ s{ ($sentence) }{\u\L$1}xmsg; print qq{'$s'}; " 'perlmonks IS a gREat site. dO you tHiNk SO? I loVE it!' 'Perlmonks is a great site. Do you think so? I love it!'
An equivalent /e substitution would be
$s =~ s{ ($sentence) }{ ucfirst lc $1 }xmsge;
Is there any significant speed difference?
|
|---|