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 Limbic~Region (Chancellor)
on Nov 17, 2003 at 10:29 UTC ( [id://307619]=note: print w/replies, xml ) Need Help??


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

Anonymous Monk,
The answer is yes, but you probably want something for the general case as well as this specific case.
#!/usr/bin/perl -w use strict; my $string = 'John is following jane'; # Specific $string =~ s/[jJ]ane/##Jane##/; # To preserve j or J in replacement # $string =~ s/([jJ]ane)/##$1##/; # More generic # $string =~ s/(John is following) (\w+)/$1 \u$2/;
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Regex: Case insensitive search but case sensitive replace
by Anonymous Monk on Nov 17, 2003 at 10:36 UTC

    Thx L~r !

    But the generic version would replace both 'jane' & 'Jane' with 'Jane'.

    I'm looking for a way to search insensitive but the replace should keep the case of the word/text ...

    Thx again !

      Anonymous Monk,
      I provided 3 examples because you were not exactly clear what your definition of "case insensitive" and "case sensitive" was in your post. The second example is closest to what you mean, but you might have also meant:
      $string =~ s/(jane)/##$1##/i;
      This would allow any letters to be any case, but be preserved in the replacement.

      Cheers - L~R

        Perfect ! That was the regex i was looking for :-)

        Guess i should use my brain next time and first read 'perlre' ;-)

        Thank you all very much !!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found