Forum Moderators: coopster & phranque

Message Too Old, No Replies

Search and replace with wildcard?

         

Jesse_Smith

5:53 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It it possible to have a wildcard in a perl search and replace code? For example

$Internal_variables{html} =~ s/whatever.cgi?blah=(.*)&more=(.*)&more2=(.*)/whatever-(.*)-more-(.*)-more2-(.*).html/g;

moltar

5:59 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.* means "any number of any characters except line feed". And it's also 'greedy'. I.e. it will match to the last possible character. In this case to the end of the string.

You need something like this:

$Internal_variables{html} =~ s/whatever.cgi?blah=([^&]+)&more=([^&]+)&more2=([^&]+)/whatever-$1-more-$2-more2-$3.html/g;
[^&]+
tells perl to match one or more any characters except '&' (or end of string). Thus it will stop matching when it hits '&'.

Putting part of regex into () will store it in $1, $2, $3 .. $n consequently.

If you want to group a part of regex, but not to 'remember' it do like this:

(:?regex)

Jesse_Smith

12:17 am on Nov 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, that almost works. I'm just having trouble with the & mark. The script uses & so the URL in HTML is & causing the URLs to not change where there are & .

I got

$Internal_variables{html} =~ s*$domainpath?Operation=ItemSearch\&SearchIndex=([^&]+)\&BrowseNode=([^&]+)*section/$1/isle/$2*g;

(I'm using * instead of / so I don't have to add a bunch of \'s.)

Which generates

/section/NAME-OF-CATEGORY&BrowseNode=229647

in the URL.

If I use just

$Internal_variables{html} =~ s*\&BrowseNode=*/isle/*g;

then the URL is correct

/section/NAME-OF-CATEGORY/isle/229647

Original URL

cgi-local/script.cgi?Operation=ItemSearch&SearchIndex=NAME-OF-CATEGORY&BrowseNode=229647

Edit: *Reads your post again.*


[^&]+ tells perl to match one or more any characters except '&' (or end of string). Thus it will stop matching when it hits '&'.

What would I change it to to make it also change the & mark?

Bluepixel

3:20 pm on Nov 22, 2004 (gmt 0)

10+ Year Member



Use

s/.*\?Operation=(.*?)&SearchIndex=(.*?)&BrowseNode=(\d+)/ $1 $2 $3/;

and read a tutorial on perl regular expressions :-)
? The question marks behind the * makes perl only match the minimum = non greedy.

kaled

4:55 pm on Nov 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I may have misunderstood something, in which case disregard the following.

If & is appearing in urls, something is going wrong somewhere. Where this encoding is used in html for urls, the browser should translate it to simply &. If the url is being created with javascript, encoding should use the escape() function to construct the query part.

Kaled.