Note that there are some explanatory texts on larger screens.

plurals
  1. POstring matching in python
    primarykey
    data
    text
    <p>I am getting trouble with the following matter.Let's say, I have some string in two list in a dictionary:</p> <pre><code> left right british 7 cuneate nucleus Medulla oblongata Motoneurons anterior </code></pre> <p>And I have some test lines in a file as like below:</p> <pre><code>&lt;s id="69-7"&gt;British Meanwhile is the studio 7 album by british pop band 10cc 7.&lt;/s&gt; &lt;s id="5239778-2"&gt;Medulla oblongata,the name refers collectively to the cuneate nucleus and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.&lt;/s&gt; &lt;s id="21120-99"&gt;Terior horn cells, motoneurons located in the spinal.&lt;/s&gt; </code></pre> <p>I want to get output as like following way:</p> <pre><code>&lt;s id="69-7"&gt;&lt;w2&gt;British&lt;/w2&gt; Meanwhile is the studio &lt;w2&gt;7&lt;/w2&gt; album by &lt;w1&gt;british&lt;/w1&gt; pop band 10cc &lt;w2&gt;7&lt;/w2&gt;.&lt;/s&gt; &lt;s id="5239778-2"&gt;&lt;w2&gt;Medulla oblongata&lt;/w2&gt;,the name refers collectively to the &lt;w1&gt;cuneate nucleus&lt;/w1&gt; and gracile nucleus, which are present at the junction between the spinal cord and the &lt;w2&gt;medulla oblongata&lt;/w2&gt;.&lt;/s&gt; </code></pre> <p>I tried with the following code:</p> <pre><code>import re def textReturn(left, right): text = "" filetext = open(text.xml, "r").read() linelist = re.split(u'[\n|\r\n]+',filetext) for i in linelist: left = left.strip() right = right.strip() if left in i and right in i: i1 = re.sub('(?i)(\s+)(%s)(\s+)'%left, '\\1&lt;w1&gt;\\2&lt;/w1&gt;\\3', i) i2 = re.sub('(?i)(\s+)(%s)(\s+)'%right, '\\1&lt;w2&gt;\\2&lt;/w2&gt;\\3', i1) text = text + i2 + "\n" return text </code></pre> <p>But it gives me:</p> <pre><code>'&lt;s id="69-7"&gt;British meanwhile is the studio &lt;w2&gt;7&lt;/w2&gt; album by &lt;w1&gt;British&lt;/w1&gt; pop band 10cc 7.&lt;/s&gt;'. &lt;s id="5239778-2"&gt;Medulla oblongata,the name refers collectively to the &lt;w1&gt;cuneate nucleus&lt;/w1&gt; and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.&lt;/s&gt; &lt;s id="21120-99"&gt;Terior horn cells, &lt;w1&gt;motoneurons&lt;/w2&gt; located in the spinal.&lt;/s&gt; </code></pre> <p><strong>i.e It can't tag if there are string at the beginning &amp; end .</strong></p> <p><strong>Also,I just want to get return those line ,which matches both left &amp; right strings, NOT others line.</strong></p> <p>Any solution please! Thanks a lot!!! </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.
 

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