Note that there are some explanatory texts on larger screens.

plurals
  1. POPython regex if group-match then put-different-character
    text
    copied!<p>I wish to transform the following: </p> <blockquote> <p>"some text <strong>http://one.two.three.source.com</strong> more text. more text more text <strong>http://source.com</strong> more text. more text <strong>http://one.source.com</strong> more text more text. more text <strong>http://one.two.source.com</strong> more text more text"</p> </blockquote> <p>To this: </p> <blockquote> <p>"some text <strong>http://one_two_three.target.com</strong> more text more text more text <strong>http://target.com</strong> more text more text <strong>http://one.target.com</strong> more text more text more text <strong>http://one_two.target.com</strong> more text more text"</p> </blockquote> <p>I wish to transform '.' separating each subdomain to '_' in a large chunk of text, the problem is that I want to make it conditional to whether there are subdomains or not. I cannot predict the rest of the text and the transformation need to occur only for url patterns. </p> <p>This is what I have so far:</p> <pre><code>src = 'source.com' dst = 'target.com' reMatch = r'http(?P&lt;a&gt;s?):(?P&lt;b&gt;\\?)/(?P&lt;c&gt;\\?)/(?P&lt;d&gt;([^.:/]+\.)?)(?P&lt;e&gt;([^.:/]+\.)?)(?P&lt;f&gt;([^.:/]+\.)?)' + src p = re.compile(reMatch, re.IGNORECASE) reReplace = r'http\g&lt;a&gt;:\g&lt;b&gt;/\g&lt;c&gt;/\g&lt;d&gt;\g&lt;e&gt;\g&lt;f&gt;' + dst p.sub(reReplace, content) </code></pre> <p>It only replaces 'source.com' with 'target.com' and copies the subdomains (up to 3) but do not replace '.' with '_' between subdomains. </p>
 

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