[ale] regexp question

Jason Day jasonday at worldnet.att.net
Wed Oct 1 13:19:29 EDT 2003


On Wed, Oct 01, 2003 at 12:13:01PM -0400, David Hamm wrote:
> I can't remember how to replace the nth occurrence of a character in a
> string using a regular expression and I can't seem to find docs that
> will jog my memory.  Could someone help me out?

Do you mean the nth *consecutive* occurrence?  If so, you can use
something like (using perl syntax):

    s/(c{5})c/$1d/

Which would change the 6th occurrence of a `c' to a `d'.

If instead you want to keep a count of the number of occurrences of a
character in an arbitrary string, and replace every occurrence after
some threshold, I don't think you can do that with a simple regex.  You
could do it with a perl script, though.
-- 
Jason Day                                       jasonday at
http://jasonday.home.att.net                    worldnet dot att dot net
 
"Of course I'm paranoid, everyone is trying to kill me."
    -- Weyoun-6, Star Trek: Deep Space 9



More information about the Ale mailing list