CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Partial substitution

 



RobL
Deleted

May 20, 2001, 4:26 AM

Post #1 of 5 (824 views)
Partial substitution Can't Post

Hi

I am sure there is a simple answer to this, but I am stumped.

I want to replace part of a string at the start and end of a line, but keep the characters in the middle intact

e.g.,

http:///www.sitename.com/78978.htm

and I want to change it to

http://www.sitename.com/directory/78978.shtml

i.e., the sequence 78978. should remain the same.

Any ideas

Rob







randor
User

May 20, 2001, 5:14 AM

Post #2 of 5 (820 views)
Re: Partial substitution [In reply to] Can't Post

try this:
$line = 'http:///www.sitename.com/78978.htm';

$line =~ s/directory\///g;

i hope this helps



randor
User

May 20, 2001, 5:19 AM

Post #3 of 5 (820 views)
Re: Partial substitution [In reply to] Can't Post

oops, sorry, i read that wrong... the answer is:

with the idea that there will be a .com/ in the line at all times try this:

$line = 'http:///www.sitename.com/78978.htm';

$line =~ s/\.com\//.com\/directory\//;
$line =~ s/\.htm/\.shtml/;

this will work as long as you know that the site will always end in .com

it is not a pretty piece, but it should do the trick.





freddo
User

May 20, 2001, 10:09 AM

Post #4 of 5 (812 views)
Re: Partial substitution [In reply to] Can't Post

Hi Rob,

here's my try at it:

Code
$ perl -de 1; 
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 1
DB<1> $dir = "mydirectory";

DB<2> $url1 = "http://www.mysite.com/78978.php";

DB<3> $url2 = $url1;

DB<4> $url2 =~ s#\/\/(.*?)/(.*)#http:\/\/\1\/$dir\/\2#;

DB<5> print $url2;
http:http://www.mysite.com/mydirectory/78978.php



(you might want to check perldoc perlre about backtracking)

I hope this helps
Freddo

;---
Real programmers don't eat cache.


freddo
User

May 20, 2001, 10:16 AM

Post #5 of 5 (811 views)
Re: Partial substitution [In reply to] Can't Post

Ooops,

didnt saw the change from .html to .shtml, the regex for that would looks like: s#\/\/(.*?)/(.*)\.htm#http:\/\/\1\/$dir\/\2.shtml#;.

take care, it would change:
http://whateversite/whatever/whatever/xxx/abc.htm into:
http://whateversite/$dir/whatever/whatever/xxx/abc.shtml

...
if it's not what you need tell me
freddo

;---
Real programmers don't eat cache.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives