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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:58:16+00:00 2026-06-13T03:58:16+00:00

Using re.sub in Python 2.7, the following example uses a simple backreference: re.sub(‘-{1,2}’, r’\g<0>

  • 0

Using re.sub in Python 2.7, the following example uses a simple backreference:

re.sub('-{1,2}', r'\g<0> ', 'pro----gram-files')

It outputs the following string as expected:

'pro-- -- gram- files'

I would expect the following example to be identical, but it is not:

def dashrepl(matchobj):
    return r'\g<0> '
re.sub('-{1,2}', dashrepl, 'pro----gram-files')

This gives the following unexpected output:

'pro\\g<0> \\g<0> gram\\g<0> files'

Why do the two examples give different output? Did I miss something in the documentation that explains this? Is there any particular reason that this behavior is preferable to what I expected? Is there a way to use backreferences in a replacement function?

  • 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-13T03:58:18+00:00Added an answer on June 13, 2026 at 3:58 am

    As there are simpler ways to achieve your goal, you can use them.

    As you already see, your replacement function gets a match object as it argument.

    This object has, among others, a method group() which can be used instead:

    def dashrepl(matchobj):
        return matchobj.group(0) + ' '
    

    which will give exactly your result.


    But you are completely right – the docs are a bit confusing in that way:

    they describe the repl argument:

    repl can be a string or a function; if it is a string, any backslash escapes in it are processed.

    and

    If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.

    You could interpret this as if “the replacement string” returned by the function would also apply to the processment of backslash escapes.

    But as this processment is described only for the case that “it is a string”, it becomes clearer, but not obvious at the first glance.

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

Sidebar

Related Questions

I am not able to replace the string (-) using re.sub in Python. >>>
I am trying to achieve the following without using sub query. For a funding,
When using a Zen sub-theme in Drupal, I get the following warnings. How can
In Excel I have attached a picture to a cell using the following Sub
Using the following code: Private Sub MakeMeSomeXmlBeforeRyanGetsAngry() Dim db As New MyDBDataContext Dim customer
I am using lxml and Python for writing XML files. I was wondering what
I'm looking for a Python library for finding the longest common sub-string from a
When using re.sub() part of re for python, a function can be used for
when I'm using following Python code to send a POST request to my Django
I have a simple pub-sub setup on a mid-sized network, using ZMQ 2.1. Although

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.