Python regex findall with all specific substrings
<p>I'm trying to get the output of the following matching regex as </p>

<p>all the Sectors for eg. ['Sector-34, Noida', 'Sec 434 Gurgaon', 'sec100']</p>

<p>P.S - sec47, \n gurgaon is the special case</p>

<p>But I suspect the output is quite weird as [('', 'tor')]</p>

<pre><code>import re

string = "Sector-34, Noida is found to be awesome place I went to eat burgers there and Sec 434 Gurgoan is also good sec100 is one the finest places for outing."

match =  re.findall(r"Sec(tor)?-?\d+\s+?\w+|Sec(tor)?\s+?\d+", string, re.IGNORECASE)

print match
</code></pre>

<p>Thanks, in Advance! </p>

<p>Here is one way that will give the expected output, but is not a general way (because you didn't provide us with general conditions):</p>

<pre><code>&gt;&gt;&gt; re.findall(r'(?:[sS]ec(?:tor)?(?:-|\s+)?\d+\W?\s+[A-Z][a-z]+)|[sS]ec(?:tor)?\d+', string)
['Sector-34, Noida', 'Sec 434 Gurgoan', 'sec100']
</code></pre>

<p>Notes:</p>

<ol>
<li><p>Here I used <code>\W</code> (none word characters) in order to match the characters like <code>,</code> in first match. If you think other none-word characters are wring you should change that to <code>,</code>.</p></li>
<li><p>We have 2 option here:</p>

<ol>
<li><code>(?:[sS]ec(?:tor)?(?:-|\s+)?\d+\W?\s+[A-Z][a-z]+)</code></li>
<li><code>[sS]ec(?:tor)?\d+</code></li>
</ol></li>
</ol>

<p>As you can see for the second part I didn't consider a word after sector and the digit, if you think that there might be a word after that you can add <code>(?:\s+[A-Z][a-z]+)?</code> after that. </p>

python regex python-2.7