Multiple Substitutions in perl

Discussion in 'Perl Misc' started by kd, Oct 16, 2005.

  1. kd

    kd Guest

    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
     
    kd, Oct 16, 2005
    #1
    1. Advertising

  2. kd wrote:
    > 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;
     
    it_says_BALLS_on_your forehead, Oct 16, 2005
    #2
    1. Advertising

  3. "kd" <> wrote in news:1129489265.610056.77780
    @f14g2000cwb.googlegroups.com:

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 16, 2005
    #3
  4. kd <> wrote:
    > 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;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 16, 2005
    #4
    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. RJGraham
    Replies:
    6
    Views:
    507
    RJGraham
    Jun 28, 2004
  2. Christopher Causer

    Multiple substitutions in one pass

    Christopher Causer, Feb 26, 2008, in forum: Ruby
    Replies:
    0
    Views:
    110
    Christopher Causer
    Feb 26, 2008
  3. Adam
    Replies:
    1
    Views:
    125
    Paul Lalli
    May 15, 2007
  4. Replies:
    4
    Views:
    124
    Ben Morrow
    Apr 4, 2008
  5. Peter Makholm

    Substitutions based on Posix ERE's in perl

    Peter Makholm, Apr 5, 2009, in forum: Perl Misc
    Replies:
    1
    Views:
    125
    smallpond
    Apr 6, 2009
Loading...

Share This Page