in reply to Short URL?

All you need to do is to run the suspected long url through one regex. Eg.

$url =~ s/^(.{35}).{3,}(.{15})$/$1...$2/;
If the url isn't too long then the regex won't match and it will remain unchanged. Otherwise an elipsis will replace the overflow characters in the middle.

Update: replaced an asterix with a plus so the elipsis will replace at least one character.

Update: changed the plus to {3,} and the 53 to 35. Thanks madhatter

-caedes

Replies are listed 'Best First'.
Re: Re: Short URL?
by The Mad Hatter (Priest) on Apr 06, 2003 at 02:14 UTC
    I think bobafifi wanted the first 35 characters, the elipsis, and the last 15 characters if the URL was over 53 characters long.

    Update The updated regex works unless the URL is exactly 53 characters long. In that case I believe bobafifi wants the URL to stay the same (although I could be wrong). Here is one that (as far as I can tell) works for any length...

    $url =~ s/^(.{35})(?=.{19}).+(.{15})$/$1...$2/;
      The updated regex works unless the URL is exactly 53 characters long. In that case I believe bobafifi wants the URL to stay the same
      In that case, the ".{3,}" in caedes' code should become ".{4,}". Indeed, I see no reason to replace the correct string of 3 characters, by 3 dots. The code would become:
      $url =~ s/^(.{35}).{4,}(.{15})$/$1...$2/;
      I would be tempted to throw in a "?" to reduce the greediness of the middle subppatern, but it wouldn't help one bit, here.
      Yeah, I'll see about fixing that up.

      -caedes