Note that there are some explanatory texts on larger screens.

plurals
  1. POPython regex chokes on \n
    primarykey
    data
    text
    <p>I wish to use a regex in Python that reads text, finds all instances in which &lt; emotion > markup exists within the same sentence as &lt; location > markup, then allows those sentences to be print to a unique line of an output file:</p> <pre><code>import re out = open('out.txt', 'w') readfile = "&lt;location&gt; Oklahoma &lt;/location&gt; where the wind comes &lt;emotion&gt; sweeping &lt;/emotion&gt; down &lt;location&gt; the plain &lt;/location&gt;. And the waving wheat. It can sure smell &lt;emotion&gt; sweet &lt;/emotion&gt;." for match in re.findall(r'(?:(?&lt;=\.)\s+|^)((?=(?:(?!\.(?:\s|$)).)*?\bemotion&gt;(?=\s|\.|$))(?=(?:(?!\.(?:\s|$)).)*?\blocation&gt;(?=\s|\.|$)).*?\.(?=\s|$))', readfile, flags=re.I): line = ''.join(str(x) for x in match) out.write(line + '\n') out.close() </code></pre> <p>The trouble is that if I read in a file that contains line breaks, the regex fails:</p> <pre><code>import re out = open('out.txt', 'w') readfile = "&lt;location&gt; Oklahoma &lt;/location&gt; where the wind \n comes &lt;emotion&gt; sweeping &lt;/emotion&gt; down &lt;location&gt; the plain &lt;/location&gt;. And the waving wheat. It can sure smell &lt;emotion&gt; sweet &lt;/emotion&gt;." for match in re.findall(r'(?:(?&lt;=\.)\s+|^)((?=(?:(?!\.(?:\s|$)).)*?\bemotion&gt;(?=\s|\.|$))(?=(?:(?!\.(?:\s|$)).)*?\blocation&gt;(?=\s|\.|$)).*?\.(?=\s|$))', readfile, flags=re.I): line = ''.join(str(x) for x in match) out.write(line + '\n') out.close() </code></pre> <p>Is there any way to modify this regular expression so that it won't choke when it hits \n? I would be most grateful for any advice others can lend on this question.</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. 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