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

  • Home
  • SEARCH
  • 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 7725355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:55:01+00:00 2026-06-01T04:55:01+00:00

I am trying to correct a text that has some very typical scanning errors

  • 0

I am trying to correct a text that has some very typical scanning errors (l mistaken for I and vice-versa). Basically I would like to have the replacement string in re.sub to depend on the number of times the ‘I’ is detected, something like that:

re.sub("(\w+)(I+)(\w*)", "\g<1>l+\g<3>", "I am stiII here.")

What’s the best way to achieve this?

  • 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-06-01T04:55:02+00:00Added an answer on June 1, 2026 at 4:55 am

    Pass a function as the replacement string, as described in the docs. Your function can identify the mistake and create the best substitution based on that.

    def replacement(match):
        if "I" in match.group(2):
            return match.group(1) + "l" * len(match.group(2)) + match.group(3)
        # Add additional cases here and as ORs in your regex
    
    re.sub(r"(\w+)(II+)(\w*)", replacement, "I am stiII here.")
    >>> I am still here.
    

    (note that I modified your regex so the repeated Is would appear in one group.)

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

Sidebar

Related Questions

I'm trying to find the correct event to listen for that will ensure that
I'm trying to figure out what the correct form of exceptions to throw would
I'm trying to understand what the correct approach for a constructor that accepts a
I'm trying to set up a site that has a carousel of divs that
Im trying to update some nested params from a form. I can see that
I've got a simple $.ajax request that I am trying to fetch some HTML
I am trying to achieve a table which has text rotated -90degrees. Up until
So, I am building a web app that has a div with text that
I am trying to display the excellent fullcalendar in an extjs window that has
I am trying to implement some very simple Data Binding in my User Control

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.