Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: regular expression search and replace

by AnomalousMonk (Archbishop)
on Nov 19, 2014 at 00:17 UTC ( [id://1107675]=note: print w/replies, xml ) Need Help??


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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1107675]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2026-01-18 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (122 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.