Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your regex matches everything from the first s to the last s, so if you replace the match with "no", you get "thinotring". </p> <p>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 <code>is a</code>. You can refer to a backreference later in the same regex using backslashes and the number of the backreference: <code>\1</code>.</p> <p>What you probably want is lookaround:</p> <pre><code>re.sub(r"(?&lt;=s ).*?(?= s)", "no", "this is a string") </code></pre> <p><code>(?&lt;=s )</code> means: Assert that it is possible to match <code>s</code> before the current position in the string, but don't make it part of the match.</p> <p>Same for <code>(?= s)</code>, but it asserts that the string will continue with <code>s</code> after the current position.</p> <p>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...<em>backreferences</em>!</p> <pre><code>re.sub(r"(s ).*?( s)", r"\1no\2", "this is a string") </code></pre> <p>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.</p> <p>If you still want to do it:</p> <pre><code>re.sub(r"(&lt;a.*?href=['"])((?!http).*?['"].*?&gt;)", r'\1http://\2', string) </code></pre> <p>might work. But this is extremely brittle.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload