JayBee has asked for the wisdom of the Perl Monks concerning the following question:
I've just started using a WYSIWYG tool and the images are broken links unless I include the "http://domain.com/" before them. I've been using the short-cut <img src="directory/image.jpg" /> method.
So to fix the bug I've tried doing a simple regex during the edit: $content=~s{(img src=")}{$1$httpURL}g; and then I planned to remove it after the form posting/submitting.
Later, realized that there is the chance of remote urls being used as well and I don't want to put two or more http-urls back to back (as they don't belong).
I basically need a good way to search and replace only image sources that don't contain "http://" already. Here's a couple of things I've tried:
my $http='http://domain.com/'; my $sample='<img src="pics/local.jpg" alt="" /> <img src="http://remote.com/pics/remote.jpg" /> more confusion, just in case: 3:00pm 12/12/12 other urls <a href="http://fake.com"> http://fake.com</a>'; #-- THIS ------------------- $sample=~s{(img src=")(\w?)[^:]}{$1$http$2}g; # something from my book, POSIX ? not very clear #-- OR --------------------- $sample=~s/(img src=")(\w.+?)(:?)/"$1$http$2"if(!$3)/eg; #-- END --------------------
I also failed on other discarded variations, so I gave up and need your help.
Any help appreciated in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Find and replace with regex
by wfsp (Abbot) on Dec 31, 2007 at 10:14 UTC | |
|
Re: Find and replace with regex
by FunkyMonk (Bishop) on Dec 31, 2007 at 10:04 UTC | |
by JayBee (Scribe) on Jan 09, 2008 at 12:05 UTC | |
by FunkyMonk (Bishop) on Jan 09, 2008 at 15:04 UTC | |
|
Re: Find and replace with regex
by ysth (Canon) on Dec 31, 2007 at 10:10 UTC | |
|
Re: Find and replace with regex
by snopal (Pilgrim) on Dec 31, 2007 at 14:53 UTC | |
by JayBee (Scribe) on Jan 09, 2008 at 12:11 UTC | |
|
Re: Find and replace with regex
by Anonymous Monk on Jan 04, 2008 at 10:07 UTC |