Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6375253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:35:31+00:00 2026-05-25T01:35:31+00:00

I am trying to replace the Nth appearance of a needle in a haystack.

  • 0

I am trying to replace the Nth appearance of a needle in a haystack. I want to do this simply via re.sub(), but cannot seem to come up with an appropriate regex to solve this. I am trying to adapt: http://docstore.mik.ua/orelly/perl/cookbook/ch06_06.htm but am failing at spanning multilines, I suppose.

My current method is an iterative approach that finds the position of each occurrence from the beginning after each mutation. This is pretty inefficient and I would like to get some input. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:35:31+00:00Added an answer on May 25, 2026 at 1:35 am

    I’ve been struggling for a while with this, but I found a solution that I think is pretty pythonic:

    >>> def nth_matcher(n, replacement):
    ...     def alternate(n):
    ...         i=0
    ...         while True:
    ...             i += 1
    ...             yield i%n == 0
    ...     gen = alternate(n)
    ...     def match(m):
    ...         replace = gen.next()
    ...         if replace:
    ...             return replacement
    ...         else:
    ...             return m.group(0)
    ...     return match
    ...     
    ... 
    >>> re.sub("([0-9])", nth_matcher(3, "X"), "1234567890")
    '12X45X78X0'
    

    EDIT: the matcher consists of two parts:

    1. the alternate(n) function. This returns a generator that returns an infinite sequence True/False, where every nth value is True. Think of it like list(alternate(3)) == [False, False, True, False, False, True, False, ...].

    2. The match(m) function. This is the function that gets passed to re.sub: it gets the next value in alternate(n) (gen.next()) and if it’s True it replaces the matched value; otherwise, it keeps it unchanged (replaces it with itself).

    I hope this is clear enough. If my explanation is hazy, please say so and I’ll improve it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replace empty field values with NULL, but can't seem to figure
I am new on sed and I am trying to replace the nth character
Trying to replace Your Price with Discount price on this page , what am
trying to replace string once and getting this error would appreciate any help. $link
I trying to replace string Resources.AppResource.Info; like this Switch(Info); Is this possible with regex?
I'm trying replace HTML5 Canvas to simple <div> But I don't know how to
Am trying to replace content with empty using this regular expression. objRegExp.Pattern = <(?>/?)(?!(p|strong)(>|\s))[^<]+?>
im trying to replace a remove unnessary new lines with preg-replace but my regex
I am trying to replace the global operator new and delete. In Linux this
I am trying to replace the nth occurrence of a character or string regardless

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.