Chris,

I found your solution to work alright, with the exception that you
probably don't want to escape | as in

   (?:http\|ftp\|file)

but only

   (?:http|ftp|file)

So that the test script below succeeds:

   use Test::More tests => 3;

   {
   my $url = "<a>http://foo.org/</a>";
   $url =~s#>((?:http|ftp|file)://.{50}).+</a>#>$1...</a>#g;
   is($url, "<a>http://foo.org/</a>");
   }

   {
   my $url = 
"<a>http://www.washingtonpost.com/wp-srv/opinions/cartoonsandvideos/toles_main.html?name=Toles&amp;date=09012006</a>";
   $url =~s#>((?:http|ftp|file)://.{50}).+</a>#>$1...</a>#g;
   is($url, 
"<a>http://www.washingtonpost.com/wp-srv/opinions/cartoonsand...</a>");
   }

   {
   my $url = 
"<a>http://www.washingtonpost.com/wp-srv/opinions/cartoonsandvideos/toles_main.html?name=Toles</a>";
   $url =~s#>((?:http|ftp|file)://.{50}).+</a>#>$1...</a>#g;
   is($url, 
"<a>http://www.washingtonpost.com/wp-srv/opinions/cartoonsand...</a>");
   }


On 9/4/06, Chris Schults <[EMAIL PROTECTED]> wrote:
I have a regular expression that is suppose to truncate long URLs at 50
characters and add "...", but can't figure out why it is not working with a
particular URL.

Here is the regex:

$url =~s#>((?:http\|ftp\|file)://.{50}).+</a>#>$1...</a>#g;

And here is the problem URL:

<a
href="http://www.washingtonpost.com/wp-srv/opinions/cartoonsandvideos/toles_
main.html?name=Toles&amp;date=09012006">http://www.washingtonpost.com/wp-srv
/opinions/cartoonsandvideos/toles_main.html?name=Toles&amp;date=09012006</a>

Here is what the regex should return:

<a
href="http://www.washingtonpost.com/wp-srv/opinions/cartoonsandvideos/toles_
main.html?name=Toles&amp;date=09012006">http://www.washingtonpost.com/wp-srv
/opinions/cartoonsand...</a>

Interestingly, the regex works fine on this modified version of the URL:

http://www.washingtonpost.com/wp-srv/opinions/cartoonsandvideos/toles_main.h
tml?name=Toles

I think the digits might have something to do with it, but not sure.

Any thoughts?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to