In comp.lang.perl.misc, Ben Bacarisse <
ben.u...@bsb.me.uk> wrote:
> George Bouras <
f...@example.com> writes:
> > spaces inside the <...> to _
> > e.g.
> > "add of <Number A> and <Number B> = " . ( <Number A> + <Number B>
> > to
> > "add of <Number_A> and <Number_B> = " . ( <Number_A> + <Number_B>
>
> s/(<[^<>]*) (?=[^<>]*>)/\1_/g
$ perl -wle '$_="<Number 100 000>"; s/(<[^<>]*) (?=[^<>]*>)/\1_/g; print'
\1 better written as $1 at -e line 1.
<Number 100_000>
> The (?= ... ) part is a "zero-width positive look ahead". It consumes
> no characters (so to speak) but must match for the space to match.
Since you force a match on '<', your code can only change one space
per <...> block. I run into this all the time with similar fixes with
vi search and replace. The fix is loop until it stops matching.
Or use another method. I like the else-thread suggested one with a tr
in an s///eg framework, like:
s/( < [^>]+ > )/ local $_ = $1; tr| |_|; $_ /xeg
I also like to not perl-golf it.
> Perl 5.30 has, experimentally, variable-length, zero-width positive look
> behind patterns (it's the variable-length part that is experimental),
> currently limited to 255 characters. That permits
>
> s/(?<=<[^<>]{0,254}) (?=[^<>]*>)/_/g
Sounds computationally expensive.
Elijah
------
tries not to use the bleeding edge features