Using regex extract all digit and word numbers
<p>I am trying to extract all string and digit numbers from  a text.</p>

<pre><code>text = 'one tweo three 10 number'
numbers = "(^a(?=\s)|one|two|three|four|five|six|seven|eight|nine|ten| \
          eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen| \
          eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty| \
          ninety|hundred|thousand)"

print re.search(numbers, text).group(0)
</code></pre>

<p>This gives me first words digit. </p>

<p>my expected result = ['one', 'two', 'three', '10']</p>

<p>How can I modify it so that all words and well digit numbers I Can get in list?</p>

<p>There are several issues here:</p>

<ul>
<li>The pattern should be used with the VERBOSE flag (add <code>(?x)</code> at the start)</li>
<li>The <code>nine</code> will match <code>nine</code> in <code>ninety</code>, so you should either put the longer values first, or use word boundaries <code>\b</code></li>
<li>Declare the pattern with a raw string literal to avoid issues like parsing <code>\b</code> as a backspace and not a word boundary</li>
<li>To match digits, you may add a <code>|\d+</code> branch to your number matching group</li>
<li>To match multiple non-overlapping occurrences of the substrings inside the input string, you need to use <code>re.findall</code> (or <code>re.finditer</code>), not <code>re.search</code>.</li>
</ul>

<p>Here is my suggestion:</p>

<pre><code>import re
text = 'one two three 10 number eleven eighteen ninety  \n '
numbers = r"""(?x)          # Turn on free spacing mode
            (
              ^a(?=\s)|     # Here we match a at the start of string before  whitespace
              \d+|          # HERE we match one or more digits
              \b            # Initial word boundary 
              (?:
                  one|two|three|four|five|six|seven|eight|nine|ten| 
                  eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen| 
                  eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty| 
                  ninety|hundred|thousand
              )             # A list of alternatives
              \b            # Trailing word boundary
)"""

print(re.findall(numbers, text))
</code></pre>

<p>See <a href="https://ideone.com/5l6rkc" rel="nofollow">Python demo</a></p>

<p>And here is a <a href="https://regex101.com/r/mQ8aF2/2" rel="nofollow">regex demo</a>.</p>

python regex