preserve case using s/ operator

Discussion in 'Perl Misc' started by Philip Primak, Jul 29, 2005.

  1. Dear pearl language users,

    I wonder if there is a way to preserve case when making changes using
    s/ operator. Here is my example: I need to change tail "abc" to "xyz",
    tail "ABC" to "XYZ" etc. When I use "s/abc *$/xyz/i" it changes all
    tail "abc" to lowercase "xyz" regardless of the original case of "abc".
    Is there an option which would allow to make it more flexible and
    preserve the case?

    Advance thanks
    Philip Primak
    Philip Primak, Jul 29, 2005
    #1
    1. Advertising

  2. Philip Primak

    Paul Lalli Guest

    Philip Primak wrote:
    > Dear pearl language users,


    Perl language
    perl interpreter
    "pearl" has to do with oysters, I believe.

    > I wonder if there is a way to preserve case when making changes using
    > s/ operator.


    What was wrong with the answer you found when you looked at the FAQ?

    $ perldoc -q case
    Found in /opt/perl/lib/5.6.1/pod/perlfaq6.pod
    How do I substitute case insensitively on the LHS while
    preserving case on the RHS?
    Paul Lalli, Jul 29, 2005
    #2
    1. Advertising

  3. On 29 Jul 2005 13:06:25 -0700
    "Philip Primak" <> wrote:

    > Dear pearl language users,
    >
    > I wonder if there is a way to preserve case when making changes using
    > s/ operator. Here is my example: I need to change tail "abc" to "xyz",
    > tail "ABC" to "XYZ" etc. When I use "s/abc *$/xyz/i" it changes all


    I don't know if you *mean* to do that but this:
    s/abc *$/xyz/i;
    ^^
    only strips trailing *spaces*, not all whitespace (as far as I know, I'm not a regexp expert). You'll probably want to use:
    s/abc\s*/xyz/i;
    to filter out all whitespaces (spaces, tabs...).

    > tail "abc" to lowercase "xyz" regardless of the original case of "abc".
    > Is there an option which would allow to make it more flexible and
    > preserve the case?


    Why don't you split it up into two statements:
    s/abc\s*$/xyz/;
    s/ABC\s*$/XYZ/;

    As far as I know you can also use conditionals in regexps but I'll leave that to a more advanced regexp user ;-).
    Sven-Thorsten Fahrbach, Jul 29, 2005
    #3
  4. On 29 Jul 2005 13:13:48 -0700
    "Paul Lalli" <> wrote:

    > Philip Primak wrote:
    > > Dear pearl language users,

    >
    > Perl language
    > perl interpreter
    > "pearl" has to do with oysters, I believe.
    >
    > > I wonder if there is a way to preserve case when making changes using
    > > s/ operator.

    >
    > What was wrong with the answer you found when you looked at the FAQ?
    >
    > $ perldoc -q case
    > Found in /opt/perl/lib/5.6.1/pod/perlfaq6.pod
    > How do I substitute case insensitively on the LHS while
    > preserving case on the RHS?


    Hmm, okay, I guess I, too, should have read that before replying. I think for simple cases, splitting the substitution up into two different statements is well sufficient, though.
    Sven-Thorsten Fahrbach, Jul 29, 2005
    #4
  5. Paul Lalli wrote:
    > Philip Primak wrote:
    >
    >>Dear pearl language users,

    >
    >
    > Perl language
    > perl interpreter
    > "pearl" has to do with oysters, I believe.


    FYI there is indeed a programming language "PEARL" (Process and
    Experiment Automation Realtime Language).
    The German wikipedia entry explicitly states that it is absolutely
    unrelated to Perl. There is even a standard for PEARL (DIN 66253).

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Aug 1, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark

    Using POST to preserve state

    Mark, Dec 13, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    417
    Andrew Robinson
    Dec 15, 2005
  2. Mark
    Replies:
    4
    Views:
    881
    Steven Cheng[MSFT]
    Feb 20, 2006
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,559
    Keith Thompson
    Feb 14, 2005
  4. Replies:
    1
    Views:
    314
    John J. Lee
    Oct 11, 2007
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    197
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page