Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this in any text editor that supports multiline regular expression search and replace.</p> <p>I used <a href="http://www.activestate.com/komodo-ide" rel="nofollow">Komodo IDE</a> to test this, because it includes an excellent regular expression tester ("Rx Toolkit") for experimenting with regular expressions. I think there are also some online tools like this. The same regular expression works in the free <a href="http://www.activestate.com/komodo-edit" rel="nofollow">Komodo Edit</a>. It should also work in most other editors that support Perl-compatible regular expressions.</p> <p>In Komodo, I used the Replace dialog with the Regex option checked, to find:</p> <pre><code>assert PyArray_TYPE\((\w+)\) == np\.NPY_DOUBLE, "\1 is not double"\s*\n\s*assert \1\.ndim == 1, "\1 has wrong dimensions"\s*\n\s*if not \(PyArray_FLAGS\(\1\) &amp; np\.NPY_C_CONTIGUOUS\):\s*\n\s*\1 = PyArray_GETCONTIGUOUS\(\1\)\s*\n\s*\1_data = &lt;double\*&gt;\1\.data </code></pre> <p>and replace it with:</p> <pre><code>\1_data = _get_data(\1, "\1") </code></pre> <p>Given this test code:</p> <pre><code> assert PyArray_TYPE(real0) == np.NPY_DOUBLE, "real0 is not double" assert real0.ndim == 1, "real0 has wrong dimensions" if not (PyArray_FLAGS(real0) &amp; np.NPY_C_CONTIGUOUS): real0 = PyArray_GETCONTIGUOUS(real0) real0_data = &lt;double*&gt;real0.data assert PyArray_TYPE(real1) == np.NPY_DOUBLE, "real1 is not double" assert real1.ndim == 1, "real1 has wrong dimensions" if not (PyArray_FLAGS(real1) &amp; np.NPY_C_CONTIGUOUS): real1 = PyArray_GETCONTIGUOUS(real1) real1_data = &lt;double*&gt;real1.data assert PyArray_TYPE(real2) == np.NPY_DOUBLE, "real2 is not double" assert real2.ndim == 1, "real2 has wrong dimensions" if not (PyArray_FLAGS(real2) &amp; np.NPY_C_CONTIGUOUS): real2 = PyArray_GETCONTIGUOUS(real2) real2_data = &lt;double*&gt;real2.data </code></pre> <p>The result is:</p> <pre><code> real0_data = _get_data(real0, "real0") real1_data = _get_data(real1, "real1") real2_data = _get_data(real2, "real2") </code></pre> <p>So how did I get that regular expression from your original code?</p> <ol> <li>Prefix all instances of <code>(</code>, <code>)</code>, <code>.</code>, and <code>*</code> with <code>\</code> to escape them (an easy manual search and replace).</li> <li>Replace the <em>first</em> instance of <code>real0</code> with <code>(\w+)</code>. This matches and captures a string of alphanumeric characters.</li> <li>Replace the remaining instances of <code>real0</code> with <code>\1</code>. This matches the text captured by <code>(\w+)</code>.</li> <li>Replace each newline and the leading space on the next line with <code>\s*\n\s*</code>. This matches any trailing space on the line, plus the newline, plus all leading space on the next line. That way the regular expression works regardless of the nesting level of the code it's matching.</li> </ol> <p>Finally, the "replace" text uses <code>\1</code> where it needs the original captured text.</p> <p>You could of course use a similar regular expression in Python if you want to do it that way. I would suggest using <code>\w</code> instead of <code>[a-z0-9]</code> just to make it simpler. Also, don't include the newlines and leading spaces; instead use the <code>\s*\n\s*</code> approach I used instead of the multiline string. This way it will be independent of the nesting level as I mentioned above.</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