Qu: REGEXP

Discussion in 'Perl Misc' started by averroes, Feb 10, 2007.

  1. averroes

    averroes Guest

    Hi,
    i have some problem with a regexp.

    i have a string like this ';;;' with no quotes
    and i want subtitute it like this ';"";"";'

    my code is :

    my $string = ';;;' ;
    $string =~ s/;;/;"";/g ;
    print $string;


    but it prints ;"";;

    thank you for your help.

    Best regards.
     
    averroes, Feb 10, 2007
    #1
    1. Advertising

  2. averroes wrote:
    > i have a string like this ';;;' with no quotes
    > and i want subtitute it like this ';"";"";'
    >
    > my code is :
    >
    > my $string = ';;;' ;
    > $string =~ s/;;/;"";/g ;
    > print $string;
    >
    > but it prints ;"";;


    Try:

    $string =~ s/;(?=;)/;""/g;

    Read about extended patterns in "perldoc perlre".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 10, 2007
    #2
    1. Advertising

  3. averroes

    Mirco Wahab Guest

    averroes wrote:
    > i have a string like this ';;;' with no quotes
    > and i want subtitute it like this ';"";"";'
    >
    > my code is :
    >
    > my $string = ';;;' ;
    > $string =~ s/;;/;"";/g ;
    > print $string;
    > but it prints ;"";;


    You're modifying pos($string) in a way that
    interferes with the pattern, so it won't
    work as intendet. You might use on of the
    following:

    my $string = ';;;';

    1) $string =~ s/;;/;"";/ while $string =~ /;;/;

    2) $string =~ s/(?<=;)(?=;)/""/g;

    3) $string = join '""', split '', $string;


    instead.

    Regards

    M.
     
    Mirco Wahab, Feb 10, 2007
    #3
  4. averroes

    averroes Guest

    Gunnar Hjalmarsson a écrit :
    > averroes wrote:
    >> i have a string like this ';;;' with no quotes
    >> and i want subtitute it like this ';"";"";'
    >>
    >> my code is :
    >>
    >> my $string = ';;;' ;
    >> $string =~ s/;;/;"";/g ;
    >> print $string;
    >>
    >> but it prints ;"";;

    >
    > Try:
    >
    > $string =~ s/;(?=;)/;""/g;
    >
    > Read about extended patterns in "perldoc perlre".
    >


    Thank you, it's working

    And thanks for the advice, i'm reading the extended patterns.

    Best regards
     
    averroes, Feb 10, 2007
    #4
  5. averroes

    Dr.Ruud Guest

    Re: REGEXP

    averroes schreef:

    > i have some problem with a regexp.
    >
    > i have a string like this ';;;' with no quotes
    > and i want subtitute it like this ';"";"";'
    >
    > my code is :
    >
    > my $string = ';;;' ;
    > $string =~ s/;;/;"";/g ;
    > print $string;
    >
    >
    > but it prints ;"";;
    >
    > thank you for your help.
    >
    > Best regards.


    You also posted this in news:alt.comp.lang.perl.

    You shouldn't multi-post.

    Use cross-posting, with the followup set to one of the groups, or even
    better: try one group, and only if that doesn't work out, try another.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Feb 11, 2007
    #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. Greg Hurrell
    Replies:
    4
    Views:
    171
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    523
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    390
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    204
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    194
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page