string conversion

T

tdelov

Hi.
I was wondering if anyone could offer any helps in a string conversion.

given the following line (this is not a real lp command)
$line = "lp -x -olandscape -ol99 -olm -x";
I'm wondering how would I change "-ol99" to "-ol=99" but not change
-olm

If I do something like this:
$line =~ s/-ol/-ol=/; # this will change -olm
How could I check the if there is a number after the "ol" characters
and only apply the change to that part of the string?

I considered using split along spaces, but prefer an actual string
substitution that works with the whole line rather than processing each
"word" in the line and then joining it again. I have to process many
files and there could be other complications if I start splitting up
each line on spaces.

Thanks.
 
X

Xicheng Jia

Hi.
I was wondering if anyone could offer any helps in a string conversion.

given the following line (this is not a real lp command)
$line = "lp -x -olandscape -ol99 -olm -x";
I'm wondering how would I change "-ol99" to "-ol=99" but not change
-olm

If I do something like this:
$line =~ s/-ol/-ol=/; # this will change -olm
How could I check the if there is a number after the "ol" characters
and only apply the change to that part of the string?

Add a lookahead construct (?=\d):

$line =~ s/-ol(?=\d)/-ol=/g;

Xicheng
 
J

J. Gleixner

Hi.
I was wondering if anyone could offer any helps in a string conversion.

given the following line (this is not a real lp command)
$line = "lp -x -olandscape -ol99 -olm -x";
I'm wondering how would I change "-ol99" to "-ol=99" but not change
-olm

If I do something like this:
$line =~ s/-ol/-ol=/; # this will change -olm
How could I check the if there is a number after the "ol" characters
and only apply the change to that part of the string?

$line =~ s/-ol(\d+)/-ol=$1/;

Read about the many adventures of Regular Expressions at any time:

perldoc perlretut
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top