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: Regular Expressions:
delete to end of string (beginner Q)

 



scarab
Deleted

Jul 5, 2000, 2:33 PM

Post #1 of 3 (3130 views)
delete to end of string (beginner Q) Can't Post

Quick question:

I want to strip the http://www. and .com/* from a string.

In other words, I want "http://www.perlarchive.com/tlc/index.shtml" to become "perlarchive". Is this possible with regular expressions?


Kanji
User / Moderator

Jul 5, 2000, 4:37 PM

Post #2 of 3 (3130 views)
Re: delete to end of string (beginner Q) [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$url = 'http://www.perlarchive.com/tlc/index.shtml';
$url =~ s<^http://www\.(.*?)\.com/.*><$1>;</pre><HR></BLOCKQUOTE>
...does what you asked for.

However, there are many, many URLs that such a substitution would be useless for such as http://www.perl.org/, http://perlguru.com/, http://216.71.167.148/, or something really wacky like http://http://www.to/ (and, yes, those all really do work).

Consider yourself warned!


errr
Deleted

Jul 5, 2000, 8:09 PM

Post #3 of 3 (3130 views)
Re: delete to end of string (beginner Q) [In reply to] Can't Post

Why use s/// when a match works better...
($url) = $url =~ m{^http://www\.(.*?)\.com}
be sure to remember that this is not the same as
$url = $url =~ m{^http://www\.(.*?)\.com}
..that one calls the match in scalar context (a true/false) instead of in list context (returns a list of the $DIGITs)

all the caveats kanji mentioned

 
 


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

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