Multiple Substitutions in perl

K

kd

Hi,

I am trying to do multiple substitutions in same line using regular
expressions.

$parameters ="V,ATP,WDR,WSD";

I am trying to replace either the pattern "U," or "V," with "U,V,"

Here is the syntax I used.

$parameters=~s/[U,]|[V,]/U,V,/;

Can anyone help me in fixing this part.

Thanks in advance.

-
Karthik
 
I

it_says_BALLS_on_your forehead

kd said:
Hi,

I am trying to do multiple substitutions in same line using regular
expressions.

$parameters ="V,ATP,WDR,WSD";

I am trying to replace either the pattern "U," or "V," with "U,V,"

Here is the syntax I used.

$parameters=~s/[U,]|[V,]/U,V,/;

Can anyone help me in fixing this part.

Thanks in advance.

try:

$parameters =~ s/(?:U|V),/U,V,/g;
 
A

A. Sinan Unur

I am trying to do multiple substitutions in same line using regular
expressions.

$parameters ="V,ATP,WDR,WSD";

I am trying to replace either the pattern "U," or "V," with "U,V,"

How is this a multiple substitution?
Here is the syntax I used.

$parameters=~s/[U,]|[V,]/U,V,/;

Did you read what [ ] means?

perldoc perlreref

[...] Matches any one of the characters contained within the
brackets
Can anyone help me in fixing this part.

#!/usr/bin/perl

use strict;
use warnings;

my $parameters = "V,ATP,WDR,WSD";
$parameters =~ s{ (?:V,) | (?:U,) }{U,V,}x;

print "$parameters\n";

__END__

Sinan
 
T

Tad McClellan

kd said:
Hi,

I am trying to do multiple substitutions in same line using regular
expressions.

$parameters ="V,ATP,WDR,WSD";

I am trying to replace either the pattern "U," or "V," with "U,V,"

Here is the syntax I used.

$parameters=~s/[U,]|[V,]/U,V,/;

Can anyone help me in fixing this part.


$parameters =~ s/[UV],/U,V,/g;
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top