Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex: Progressive matching and replace

Reply
Thread Tools

Regex: Progressive matching and replace

 
 
Vito Corleone
Guest
Posts: n/a
 
      01-24-2005
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
 
Reply With Quote
 
 
 
 
Martin Kissner
Guest
Posts: n/a
 
      01-24-2005
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)
 
Reply With Quote
 
 
 
 
Vito Corleone
Guest
Posts: n/a
 
      01-24-2005
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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-24-2005
Vito Corleone <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert progressive jpg to non-progressive jpg Sniper Abandon Ruby 0 05-15-2009 10:42 AM
How to output progressive video from a video card to a progressive CRT TV? kopn@hotbox.ru DVD Video 4 09-13-2006 07:22 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM
Progressive scan dvd's on a non-progressive scan tv jack lift DVD Video 7 12-09-2003 06:01 PM



Advertisments