title: Extract number inside brackets using regular expression?
body: <p>Suppose I have a data frame like this one:</p>
  
  <pre><code>library(tidyverse)
  index &lt;- 1:1000
  df1 &lt;- data.frame(index = glue::glue('index[{index}]'),
                    X = rnorm(1000))
  </code></pre>
  
  <p>I want to <code>mutate</code> index so it is a numeric variable with the number inside the brackets. I can do that with this code:</p>
  
  <pre><code>df2 &lt;- df1 %&gt;% mutate(index = gsub(pattern = 'index[', replacement = '', x = index, fixed = T),
                        index = gsub(pattern = ']', replacement = '', x = index, fixed = T),
                        index = as.numeric(index))
  </code></pre>
  
  <p>I'm sure there is a better way of doing this with regular expressions. Ideally, I would like something that is agnostic to the text before <code>[</code>. </p>