Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Regex: Case insensitive search but case sensitive replace

by Abigail-II (Bishop)
on Nov 17, 2003 at 12:26 UTC ( #307638=note: print w/ replies, xml ) Need Help??


in reply to Regex: Case insensitive search but case sensitive replace

Well, replacing 'Jane' (any case) with '##Jane##' (same case) is trivial: s/(jane)/##$1##/ig; will do.

But let's say you want to replace 'Jane' (any case) with 'Mary' (same case). Here's one way of doing it:

#!/usr/bin/perl use strict; use warnings; my $pat = "jane"; my $repl = "mary"; while (<DATA>) { s/($pat)/$1 ^ $pat ^ $repl/eig; print; } __DATA__ jane janE jaNe jaNE jAne jAnE jANe jANE Jane JanE JaNe JaNE JAne JAnE JANe JANE mary marY maRy maRY mAry mArY mARy mARY Mary MarY MaRy MaRY MAry MArY MARy MARY

Abigail

Comment on Re: Regex: Case insensitive search but case sensitive replace
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2016-04-30 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW







    Results (441 votes). Check out past polls.