Someone wanted to do this in alt.perl recently. I don't know why. It generated a bunch of weird solutions, so I posted my own straightforward one.
s{(<.*?>)}{ my $x = $1; $x =~ tr/ /_/; $x; }eg;

Replies are listed 'Best First'.
Re: replace all spaces within anglebrackets with underscores
by danger (Priest) on Jan 03, 2001 at 22:50 UTC

    Wouldn't it be a smidgen better to use a negative char class and a + quantifier?

    s{(<[^>]+>)}{ (my $x = $1) =~ tr/ /_/; $x; }eg;
Re: replace all spaces within anglebrackets with underscores
by chipmunk (Parson) on Jan 04, 2001 at 00:29 UTC
    This probably counts as a weird solution: s/ (?=[^<]*>)/_/g It only works for balanced, non-nested angle brackets, though. :) (Inspired by Uri Guttman.)
Re: replace all spaces within anglebrackets with underscores
by Dominus (Parson) on Jan 05, 2001 at 01:20 UTC
    This reminds me a of a similar thing I did in Apache::AddrMunge, which is an Apache plugin module I wrote that mangles my mail address on my web pages.

    The code looks like this:

    s{\b(mjd[-\w]*) \@ ( (?:\w+\.)? plover\.com \b) } {($a,$b) = ($1,$2); $a =~ m{-subscribe$} ? "$a\@$b" : "$a-id-$id\@$b" }gex;
    It turns an address like mjd-perl-pause@www.plover.com into mjd-perl-pause-id-iws+bx9zvct@www.plover.com, unless the address is a mailing list subscribe address such as mjd-perl-memoize-subscribe@plover.com.

    This page has a detailed description of a similar solution to a problem, but if you read it, be sure to start here instead and read backwards up the page. The end result:

    s{(.*?)(\`([^\']*)\')} {$pos += length $1; my $length = length $2; print ((' ' x ($pos)), "^", (' ' x ($length-2)), "^ ? "); my $r = lc <STDIN>; $pos += $length; (substr($r,0,1) eq 'n') ? $& : "$1<tt>$3</tt>" }eg;
      I knew you munged your email addresses, but I never actually read this before. I must say, I find this very clever.

      Cheers,
      KM