On Sep 7, Dan said:

>If I have a string with say a number of '>>>>>>' in it which may be
>variable in length and I wish to write a regexp to match and replace it
>with the equivalent number of ')'s [ie >>>> would become ))))]

There's a very clever solution to this in "Effective Perl
Programming".  It is specifically for LEADING zeroes:

  s/\G0/ /g;

That turns all leading zeroes in $_ to spaces.

Yours might not be leading, so you must first "prep" your string:

  /[^>]*/g;   # set pos($_) to right before the first >, if there is one
  s/\G>/)/g;  # change all consecutive >'s from this point on to )'s

That will work.  You can also do:

  s/(>+)/")" x length($1)/g;

That one probably works better, too -- it finds ALL runs.  You'd have to
massage that first approach quite a bit to get it to work this way.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to