Python version of Java regular expression?
<p>I am a Java developer, and new to Python. I would like to define a regex accepting all the alphabetic characters except for some of them. I want to exclude just the vowels and the character 'y', be it in upper- or lowercase. </p>

<p>The regex in Java for it would be as follows:</p>

<pre><code> "[a-zA-Z&amp;&amp;[^aeiouyAEIOUY]]"
</code></pre>

<p>How can I (re)define it as in Python? The above doesn't work for Python, obviously. And I also would <strong>NOT</strong> like the following pattern to be suggested:</p>

<pre><code>"[bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ]"
</code></pre>

<p>I don't think the current python regular expression module has exactly what you're looking for. The eventual replacement <a href="https://pypi.python.org/pypi/regex" rel="nofollow"><code>regex</code></a> does have what you need, and you can install it should you wish.</p>

<p>Other than that, a negation might be the way to go. Basically, define all the characters you don't want and then invert that. Sounds labourious, but the "not-word" shorthand (<code>\W</code>) can help us out. <code>\w</code> means <code>a-zA-Z0-9_</code> (for ASCII matches), and <code>\W</code> means the opposite (<code>[^\w]</code>). Thus, <code>[aeiouyAEIOUY\W\d_]</code> means every character which you are not looking for, and so <code>[^aeiouyAEIOUY\W\d_]</code> means every character you are looking for. eg.</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; s = "xyz_ d10 word"
&gt;&gt;&gt; pattern = "[^aeiouyAEIOUY\W\d_]+"
&gt;&gt;&gt; re.findall(pattern, s)
['x', 'z', 'd', 'w', 'rd']
</code></pre>

<p>If you are strictly after only ASCII characters then you can use the <code>ASCII</code> flag. eg.</p>

<pre><code>&gt;&gt;&gt; s = "EspaÃ±ol"
&gt;&gt;&gt; re.findall(pattern, s)
['sp', 'Ã±', 'l']
&gt;&gt;&gt; re.findall(pattern, s, re.ASCII)
['sp', 'l']
</code></pre>

python regex