title: How to extract numbers included in a string in order using regex
body: <p>I have a data frame like the one shown below:</p>
  <pre><code>df &lt;- data.frame(col = c(&quot;3.2% 1ST $100000 AND 1.1% BALANCE&quot;, &quot;3.3% 1ST $100000 AND 1.2% BALANCE AND $3000 BONUS FULL PRICE ONLY&quot;, 
                           &quot;$4000&quot;, &quot;3.3% 1ST $100000 AND 1.2% BALANCE&quot;, &quot;3.3% 1ST $100000 AND 1.2% BALANCE&quot;, 
                           &quot;3.2% 1ST $100000 1.1% BALANCE&quot;,&quot;2.1% 1ST $100000 AND 1.2% BALANCE PLUS $2500&quot;))
  
                                                                  col
  1                                 3.2% 1ST $100000 AND 1.1% BALANCE
  2 3.3% 1ST $100000 AND 1.2% BALANCE AND $3000 BONUS FULL PRICE ONLY
  3                                                             $4000
  4                                 3.3% 1ST $100000 AND 1.2% BALANCE
  5                                 3.3% 1ST $100000 AND 1.2% BALANCE
  6                                     3.2% 1ST $100000 1.1% BALANCE
  7                      2.1% 1ST $100000 AND 1.2% BALANCE PLUS $2500 
  </code></pre>
  <p>What I wanted to do is to separate numbers in these string and put them in a different columns of the new data frame. As @Ronak Shah recommended here:<a href="https://stackoverflow.com/questions/64780126/how-to-find-a-pattern-in-a-string-and-extract-it-as-a-new-column-of-data-frame">How to find a pattern in a string and extract it as a new column of data frame</a></p>
  <p>I used this method, which works perfectly:</p>
  <pre><code>library(tidyverse)
  
      a&lt;-df %&gt;%
        extract(col, c('First', 'cut-off', 'Second'), 
                '(\\d+.*?)% 1ST\\s*\\$(\\d+).*?(\\d+.*?)%.*?', remove = FALSE) %&gt;%
        mutate(Bonus = str_extract(col, '\\d+(?=\\sBONUS)')) 
  </code></pre>
  <p>However, I just realized that sometimes for some reason, the word <strong>BONUS</strong> is not mentioned in the comments while the number is actually a BONUS. For example in this string <code>2.1% 1ST $100000 AND 1.2% BALANCE PLUS $2500</code> the forth number is BONUS but it is not followed by the word &quot;BONUS&quot;, so the number can't be captured.
  I am wondering if there is any way to solve this? is there any way to extract the fourth number of the string? It seems that in most cases this BONUS number is the fourth number of my string.</p>