Ordered/Prioritized return of a regular expression
<p>I have the following group of possible values that can appear in a field of a DataFrame (extract from a database):</p>

<p>(N2|N1|N11|N12|N3|N4|N6|N10|N13|N5|N7|N8|N9)</p>

<p>The field can contain any of the above in any combination, for example:</p>

<p>"N1, N6, N9"</p>

<p>I want to extract from every element of the field <strong>only</strong> the one with highest "rank" meaning N9>N8>N7>N5... according to the order of my group above.</p>

<p>So from the example it would return "N9". For "N1, N3, N11" it would return "N3".</p>

<p>Is this possible with RegEx? I am using Python/Pandas on this.</p>

<p>Thanks a lot in advance!</p>

<p>Considering you have a dataframe <code>df</code> with your column of data named <code>data</code>, here is a simple way without using regex. Split the strings into columns, then sort the resulting list and take the first element:</p>

<pre><code>df.data.str.split(',').apply(lambda l: sorted(l, reverse=True)[0])
Out[7]: 
0    N9
1    N3
Name: data, dtype: object
</code></pre>

python regex pandas