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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:07:17+00:00 2026-05-16T23:07:17+00:00

I could really use some help with a Python regular expression problem. You’d expect

  • 0

I could really use some help with a Python regular expression problem. You’d expect the result of

import re
re.sub("s (.*?) s", "no", "this is a string") 

to be “this is no string”, right? But in reality it’s “thinotring”. The sub function uses the entire pattern as the group to replace, instead of just the group I actually want to replace.

All re.sub examples deal with simple word replacement, but what if you want to change something depending on the rest of the string? Like in my example…

Any help would be greatly appreciated.

Edit:

The look-behind and look-forward tricks won’t work in my case, as those need to be fixed width. Here is my actual expression:

re.sub(r"<a.*?href=['\"]((?!http).*?)['\"].*?>", 'test', string)

I want to use it to find all links in a string that don’t begin with http, so I can but a prefix in front of those links (to make them absolute rather then relative).

  • 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-16T23:07:18+00:00Added an answer on May 16, 2026 at 11:07 pm

    Your regex matches everything from the first s to the last s, so if you replace the match with “no”, you get “thinotring”.

    The parentheses don’t limit the match, they capture the text matched by whatever is inside them in a special variable called backreference. In your example, backreference number 1 would contain is a. You can refer to a backreference later in the same regex using backslashes and the number of the backreference: \1.

    What you probably want is lookaround:

    re.sub(r"(?<=s ).*?(?= s)", "no", "this is a string")
    

    (?<=s ) means: Assert that it is possible to match s before the current position in the string, but don’t make it part of the match.

    Same for (?= s), but it asserts that the string will continue with s after the current position.

    Be advised that lookbehind in Python is limited to strings of fixed length. So if that is a problem, you can sort of work around this using…backreferences!

    re.sub(r"(s ).*?( s)", r"\1no\2", "this is a string")
    

    OK, this is a contrived example, but it shows what you can do. From your edit, it’s becoming apparent that you’re trying to parse HTML with regex. Now that is not such a good idea. Search SO for “regex html” and you’ll see why.

    If you still want to do it:

    re.sub(r"(<a.*?href=['"])((?!http).*?['"].*?>)", r'\1http://\2', string)
    

    might work. But this is extremely brittle.

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

Sidebar

Related Questions

I've been fighting this problem for many hours now and could really use some
I am stumped and could really use some help with this gallery I've been
I can't get my head around this and I could really use some help.
I have just started learning python/matplotlib/basemap and could really use some help. How do
Is this possible? If so, I could really use some help on this. I'm
I could really use some help here. I just created a new bare repo
I am really new to Ruby and could use some help with a program.
I'am new to Python 3 and could really use a little help. I have
I could really use some help optimizing a table on my website that is
I have been struggling with this for a while and could really use some

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.