Regex: Progressive matching and replace

V

Vito Corleone

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
 
M

Martin Kissner

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
 
V

Vito Corleone

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.
 
T

Tad McClellan

Vito Corleone said:
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?

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?
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top