title: Create a regular expression that continues to extract information if and only if the following pattern is met using it as a condition
body: <pre class="lang-py prettyprint-override"><code>import re
  
  def fun(x):
      match=re.search(r&quot;(?&lt;=hay que) ([\w\s,]+) ([\w\s]+)&quot;,x)
      if match:
          for i in re.split(',|y',match[1]):
              with open(f'{i}.txt','w') as file:
                  file.write(match[2])
  
  
  input_text = str(input())
  fun(input_text)
  </code></pre>
  <p>I need to create a regular expression that continues to match only if there is a comma <code>,</code> or <code>y</code> after the last one. Extracting the words and creating a text file as indicated in the following examples. And in case these words are followed by <code>,</code> or <code>y</code>, continue extracting. Then the end of the sentence must be written on one line of each of the .txt files created.</p>
  <p>I was having trouble with sentences like for example:</p>
  <p><code>hay que pintar y decorar las paredes de ese lugar</code></p>
  <p>generate: pintar.txt, decorar las paredes de ese.txt</p>
  <p>write inside each of them: <code>lugar</code></p>
  <p><strong>but it should be:</strong></p>
  <p>generate: pintar.txt, decorar.txt</p>
  <p>write inside each of them: <code>las paredes de ese lugar</code></p>
  <hr />
  <p>Other Examples...</p>
  <p>input_sense: <code>hay que pintar y decorar las paredes y los techos</code></p>
  <p>generate: pintar.txt, decorar.txt</p>
  <p>write inside each of them: <code>las paredes y los techos</code></p>
  <hr />
  <p>input_sense: <code>hay que correr, saltar y cantar para llegar alli</code></p>
  <p>generate: correr.txt , saltar.txt, cantar.txt</p>
  <p>write inside each of them: <code>para llegar alli</code></p>
  <hr />
  <p>input_sense: <code>yo creo que hay que saltar y correr para ir a ese lugar</code></p>
  <p>generate: saltar.txt, correr.txt</p>
  <p>write inside each of them: <code>para ir a ese lugar</code></p>
  <hr />
  <p><strong>IMPORTANT:</strong>  And in case the words listed begin with <code>no ser|ser|no</code></p>
  <p>input_sense: <code>hay que esconderse y ser silenciosos para no ser descubiertos</code></p>
  <p>generate: esconderse.txt, ser_silenciosos.txt</p>
  <p>write inside each of them: <code>para no ser descubiertos</code></p>
  <hr />
  <p>input_sense: <code>hay que trabajar, escalar y no temer si quieres llegar a la meta</code></p>
  <p>generate: trabajar.txt, escalar.txt, no_temer.txt</p>
  <p>write inside each of them: <code>si quieres llegar a la meta</code></p>
  <hr />