I do not get what expected on a regex expression
<p>I am looking for words in text6 that either contain a z or the sequence of characters pt or end with ize</p>

<p>I wrote the following but it includes many words that do not meet the above criteria like appease,dance,offensive,executive.... Why is this happening?</p>

<pre><code>L2=[w for w in text6 if re.search(r".*[z]|.*[p][t]|[ize]$",w)
</code></pre>

<p>Another question building up on the previous exercise: I need to add a new alternative condition i.e. that the words starts with 1 and only 1 capital letter.</p>

<p>I wrote</p>

<pre><code>L2=[w for w in text6 if re.search(r"[A-Z]{1}|.*[z]|.*[p][t]|[ize]$",w)
</code></pre>

<p>It includes also upper case words (i.e all characters in capital letter)</p>

<p>Could any one help in these 2 questions?</p>

<p>Thank you in advance</p>

<p>mauro</p>

<p>What you are looking for is:</p>

<pre><code>[w for w in text6 if re.search(r"z|pt|ize$", w)]
</code></pre>

<p>This will capture all the required words. Note that, the last part is not required as any word matching <code>ize$</code> will also match <code>z</code>. So essentially, the expression boils down to:</p>

<pre><code>[w for w in text6 if re.search(r"z|pt", w)]
</code></pre>

<p>The second case can be solved by using the expression <code>^[A-Z]{1}[^A-Z]</code>. That is,</p>

<ul>
<li>Starts with exactly one capital letter</li>
<li>Isn't followed by a capital letter</li>
</ul>

<p>This is used below:</p>

<pre><code>[w for w in text6 if re.search(r"^[A-Z]{1}[^A-Z]|z|pt|ize$", w)]
</code></pre>

<p>Or, simply,</p>

<pre><code>[w for w in text6 if re.search(r"^[A-Z]{1}[^A-Z]|z|pt", w)]
</code></pre>

python regex