preserve case using s/ operator

P

Philip Primak

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
 
P

Paul Lalli

Philip said:
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?
 
S

Sven-Thorsten Fahrbach

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 ;-).
 
S

Sven-Thorsten Fahrbach

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


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.
 
J

Josef Moellers

Paul said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top