Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your expression is working just fine; Python is just showing you a <em>string literal</em> instead, where you'd have to escape quotes to be able to re-use it as a python string.</p> <p>If you print the value, no such escaping takes place:</p> <pre><code>&gt;&gt;&gt; re.sub( 'src=\"(.+)\"', 'src="{{ \'\\1\' | asset_url }}"', test, flags=re.MULTILINE ) '\n &lt;script type="text/javascript" src="{{ \'./js/jquery-1.8.3.min.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;!--&lt;script src="{{ \'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript\' | asset_url }}"&gt;&lt;/script&gt;--&gt;\n &lt;script type="text/javascript" src="{{ \'./js/ie-amendments.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;script type="text/javascript" src="{{ \'./js/jquery.bxslider.min.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;script type="text/javascript" src="{{ \'./js/jquery.colorbox-min.js\' | asset_url }}"&gt;&lt;/script&gt;\n &lt;script type="text/javascript" src="{{ \'./js/main.js\' | asset_url }}"&gt;&lt;/script&gt;\n' &gt;&gt;&gt; print(re.sub( 'src=\"(.+)\"', 'src="{{ \'\\1\' | asset_url }}"', test, flags=re.MULTILINE )) &lt;script type="text/javascript" src="{{ './js/jquery-1.8.3.min.js' | asset_url }}"&gt;&lt;/script&gt; &lt;!--&lt;script src="{{ 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript' | asset_url }}"&gt;&lt;/script&gt;--&gt; &lt;script type="text/javascript" src="{{ './js/ie-amendments.js' | asset_url }}"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="{{ './js/jquery.bxslider.min.js' | asset_url }}"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="{{ './js/jquery.colorbox-min.js' | asset_url }}"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="{{ './js/main.js' | asset_url }}"&gt;&lt;/script&gt; </code></pre> <p>You can use <code>?</code> to make <code>+</code>, <code>*</code> and <code>?</code> qualifiers <em>non-greedy</em>; matching the minimum instead of the maximum. You can also match anything that is <em>not</em> a quote instead:</p> <pre><code>r'src="([^"]+)"' </code></pre> <p>which constrains that part of the regular expression much better; <code>[^"]</code> matches any character that is <em>not</em> a double quote.</p> <p>When specifying a regular expression pattern, it is generally a good idea to use a python raw string literal (<code>r''</code>) instead, saves a lot of headaches as to what needs escaping and what does not. Using a raw string literal, your substitution pattern can be simplified to:</p> <pre><code>r'src="{{ \'\1\' | asset_url }}"' </code></pre> <p>for a final line of:</p> <pre><code>re.sub(r'src="([^"]+)"', r'src="{{ \'\1\' | asset_url }}"', test, flags=re.MULTILINE) </code></pre>
 

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