Help please with Perl + regular expressions
Posted by Ritchie (Ritchie), 8 January 2004I'm not the best skilled in Perl, and here is my problem....
I need to search through an HTML page and find all occurences of something like:
and replace them to become
Where the path and script name are in a variable: $script_name = "/dev/tester.cgi"
and the replacement path in a variable: $short_script_name = "/dev/test"
There may be more or less entries in the query string, but they will always be in pairs.
I have come up with this:
$htmlpage =~ s|<a href="($script_name)/([?])\w+([=])\w+([&])\w+"></a>|$short_script_name / / /| /ge;
Does that look as though it will work ??
Posted by admin (Graham Ellis), 9 January 2004The way to test out whether something works is to try it out on a test data set .... I look at your code and I think it's more complex that it needs to be (and I think the "e" modifier is wrong); I have a few other concerns too. Here is a sample piece of code that might help you:
And that runs and produces:
Which as far as I can see is what you're looking for.
a) Don't try to do it all in one regular expression - two simple ones are much easier than one complex one
b) The "g" modifier isn't going to work for you as you have a series of overlapping matches - so I've used a while loop.
Posted by Ritchie (Ritchie), 9 January 2004on 01/09/04 at 13:49:37, Graham Ellis wrote:
Many thanks for your reply. I will attempt to see how that works in my scenario.
I agree with what you say about doing it in one go. Since my post I have refined the script to look like......
That works, but I will try yours, but I need to understand it first
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho