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 7308687
In Process

The Archive Base Latest Questions

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

For doing a regex substitution, there are three things that you give it: The

  • 0

For doing a regex substitution, there are three things that you give it:

  • The match pattern
  • The replacement pattern
  • The original string

There are three things that the regex engine finds that are of interest to me:

  • The matched string
  • The replacement string
  • The final processed string

When using re.sub, the final string is what’s returned. But is it possible to access the other two things, the matched string and replacement string?

Here’s an example:

orig = "This is the original string."
matchpat = "(orig.*?l)"
replacepat = "not the \\1"

final = re.sub(matchpat, replacepat, orig)
print(final)
# This is the not the original string

The match string is "original" and the replacement string is "not the original". Is there a way to get them? I’m writing a script to to search and replace in many files, and I want it to print it what it’s finding and replacing, without printing out the entire line.

  • 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-28T23:35:32+00:00Added an answer on May 28, 2026 at 11:35 pm
    class Replacement(object):
    
        def __init__(self, replacement):
            self.replacement = replacement
            self.matched = None
            self.replaced = None
    
        def __call__(self, match):
            self.matched = match.group(0)
            self.replaced = match.expand(self.replacement)
            return self.replaced
    
    >>> repl = Replacement('not the \\1')
    >>> re.sub('(orig.*?l)', repl, 'This is the original string.')
        'This is the not the original string.'
    >>> repl.matched
        'original'
    >>> repl.replaced
        'not the original'
    

    Edit: as @F.J has pointed out, the above will remember only the last match/replacement. This version handles multiple occurrences:

    class Replacement(object):
    
        def __init__(self, replacement):
            self.replacement = replacement
            self.occurrences = []
    
        def __call__(self, match):
            matched = match.group(0)
            replaced = match.expand(self.replacement)
            self.occurrences.append((matched, replaced))
            return replaced
    
    >>> repl = Replacement('[\\1]')
    >>> re.sub('\s(\d)', repl, '1 2 3')
        '1[2][3]'
    
    >>> for matched, replaced in repl.occurrences:
       ....:     print matched, '=>', replaced
       ....:     
     2 => [2]
     3 => [3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a regex expression that I'm doing a split against another string and
i'm doing the simplest regex.match ever, i am giving the Regex.Match a pattern of
I have a Python 3 script that is going to be doing some regex
Can someone tell me exactly what is this regex doing? new StringTokenizer(string,:/.@\\;,+ ); I
How to put together a regex that will match words that end with "Id"
I am doing the regex in JavaScript and the response from youtube give me
I have a very simple CLR Function for doing Regex Matching public static SqlBoolean
While doing some small regex task I came upon this problem. I have a
I know the regex for doing a global replace, %s/old/new/g How do you go
Another noob regex problem/question. I'm probably doing something silly so I thought I'd exploit

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.