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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:59:35+00:00 2026-06-04T01:59:35+00:00

I am tring to go a simple regex replace on a string in python.

  • 0

I am tring to go a simple regex replace on a string in python. This is my code:

>>> s = "num1 1 num2 5"
>>> re.sub("num1 (.*?) num2 (.*?)","1 \1 2 \2",s)

I would expect an output like this, with the \numbers being replaced with their corresponding groups.

'1 1 2 5'

However, this is the output I am getting:

'1 \x01 2 \x025'

And I’m kinda stumped as to why the \x0s are their, and not what I would like to be there. Many thanks for any help

  • 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-04T01:59:36+00:00Added an answer on June 4, 2026 at 1:59 am

    You need to start using raw strings (prefix the string with r):

    >>> import re
    >>> s = "num1 1 num2 5"
    >>> re.sub(r"num1 (.*?) num2 (.*?)", r"1 \1 2 \2", s)
    '1 1 2 5'
    

    Otherwise you would need to escape your backslashes both for python and for the regex, like this:

    >>> re.sub("num1 (.*?) num2 (.*?)", "1 \\1 2 \\2", s)
    '1 1 2 5'
    

    (this gets really old really fast, check out the opening paragraphs of the python regex docs

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

Sidebar

Related Questions

I am trying to do a simple replacement utilizing the static RegEx.Replace method and
I'm trying to write a simple PHP function that can take a string like
After finding the fastest string replace algorithm in this thread , I've been trying
Given a literal string such as: Hello\n\n\n\n\n\n\n\n\n\n\n\nWorld I would like to reduce the repeated
I'm having a little trouble comprehending this simple use of the /e regex modifier.
I'm trying to make this simple code of creating 3 multiple-choice question out of
I'm using javascript with a super simple regex to replace a < with the
I have a command output like this hdisk10 128000 200B75Y4191001707210790003IBMfcp hdisk11 128000 200B75Y4191001807210790003IBMfcp hdisk13
I am trying to write a simple regex to match a percentage value range
I'm trying to use Python 2.7 regex's to retrieve data from sample web pages

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.