Regex: Progressive matching and replace

Discussion in 'Perl Misc' started by Vito Corleone, Jan 24, 2005.

  1. Hi,

    How can I do progressive progressive matching and replace? For example I
    want to change this text:
    ##--- before ---##
    adfdf <photo src=12> dasfd Fdfdsfdf <photo
    src=14> adsfdf dsf dsf dsf asdfdsa ds <phot
    o> dsafd adsf

    to:

    ##--- after ---##
    adfdf <img src=12.gif> dasfd Fdfdsfdf <img
    src=14.gif> adsfdf dsf dsf dsf asdfdsa ds <phot
    o> dsafd adsf

    And also I want to check if each user has access to the image they are
    trying to link to. Here is what I tried:

    ##----- start sample.pl -----##
    my $test =<< "BLOCK"; ## User input this from web interface, textarea
    adfdf <photo src=12> dasfd Fdfdsfdf <photo
    src=14> adsfdf dsf dsf dsf asdfdsa ds <phot
    o> dsafd adsf
    BLOCK
    ;

    $test =~ s/\n//g;
    while ($test =~ /<photo src=(\d+)>/g) {
    if (!check_access($1)) { ## See if user have access to this photo
    die "Use your own photo";
    }

    ## Here I don't know how to change
    ## from <photo src=XX> to <img src=XX.gif>
    }
    ##----- end sample.pl -----##

    But I have some problems here:
    1. I can do progressive matching, but I don't know how to replace it.
    2. Even if I can, I don't know how can I restore the newline that I
    deleted.

    Any better way to do this? Thank you very much.

    --Vito
    Vito Corleone, Jan 24, 2005
    #1
    1. Advertising

  2. Vito Corleone wrote :
    > Hi,
    >
    > How can I do progressive progressive matching and replace? For example I
    > want to change this text:
    > ##--- before ---##
    > adfdf <photo src=12> dasfd Fdfdsfdf <photo
    > src=14> adsfdf dsf dsf dsf asdfdsa ds <phot
    > o> dsafd adsf
    >
    > to:
    >
    > ##--- after ---##
    > adfdf <img src=12.gif> dasfd Fdfdsfdf <img
    > src=14.gif> adsfdf dsf dsf dsf asdfdsa ds <phot
    > o> dsafd adsf


    #!/usr/bin/perl

    use strict;
    use warnings;

    my $string ="adfdf <photo src=12> dasfd Fdfdsfdf <photo src=14> adsfdf
    dsf dsf dsf asdfdsa ds <photo> dsafd adsf";

    $string =~ s/photo src=(\d*)/img src=$1.gif/g;

    print "$string\n";

    Output:
    iadfdf <img src=12.gif> dasfd Fdfdsfdf <img src=14.gif> adsfdf dsf dsf
    dsf asdfdsa ds <photo> dsafd adsf

    HTH
    Martin

    --
    Epur Si Muove (Gallileo Gallilei)
    Martin Kissner, Jan 24, 2005
    #2
    1. Advertising

  3. Sorry this is not clear enough. I give some more examples here.

    > ##--- before ---##
    > adfdf <photo src=12> dasfd Fdfdsfdf <photo
    > src=14> adsfdf dsf dsf dsf asdfdsa ds <phot
    > o> dsafd adsf
    >
    > to:
    >
    > ##--- after ---##
    > adfdf <img src=12.gif> dasfd Fdfdsfdf <img
    > src=14.gif> adsfdf dsf dsf dsf asdfdsa ds <phot
    > o> dsafd adsf



    Other:

    ##-- before --##
    aaa aaaa <photo src=12> adfafdsfd <photo
    src=24> adfa dfdf

    ##-- after --##
    aaa aaaa <img src=12.gif> adfafdsfd
    adfa dfdf

    This user doesn't have access to 24.gif, so we just delete it.
    Vito Corleone, Jan 24, 2005
    #3
  4. Vito Corleone <> wrote:

    > How can I do progressive progressive matching and replace?



    By using the s/// operator.


    > my $test =<< "BLOCK"; ## User input this from web interface, textarea
    > adfdf <photo src=12> dasfd Fdfdsfdf <photo
    > src=14> adsfdf dsf dsf dsf asdfdsa ds <phot
    > o> dsafd adsf
    > BLOCK
    > ;
    >
    > $test =~ s/\n//g;



    Did you try print()ing $test at this point?

    Did you see a part of the string that looked like "<photosrc=14>"?


    > while ($test =~ /<photo src=(\d+)>/g) {



    The substring above will not match that pattern, you'll miss it.


    > ## Here I don't know how to change
    > ## from <photo src=XX> to <img src=XX.gif>



    s/<photo src=(\d+)>/<img src=$1.gif>/g;



    > 2. Even if I can, I don't know how can I restore the newline that I

    ^^^^^^^^^^^
    > deleted.



    "the newline" is singular, yet you deleted more than one newline.

    Which newline did you want to undelete?

    Why delete them in the first place if you don't want them deleted?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 24, 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. wilson k. tsugue

    how to not to show progressive content in browser

    wilson k. tsugue, Aug 20, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    324
    Kevin Spencer
    Aug 20, 2003
  2. Xah Lee
    Replies:
    1
    Views:
    928
    Ilias Lazaridis
    Sep 22, 2006
  3. Xah Lee
    Replies:
    8
    Views:
    455
    Ilias Lazaridis
    Sep 26, 2006
  4. Xah Lee
    Replies:
    2
    Views:
    209
    Xah Lee
    Sep 25, 2006
  5. Replies:
    2
    Views:
    382
Loading...

Share This Page