Selecting digits from messy vector via gsub / regex
<p>I'm working with a vector <em>vecA</em> corresponding to the one generated below:</p>

<pre><code>vecA &lt;- c("[  0, 10)", "[ 10, 20)", "[ 20, 30)", "[ 50, 60)", "[ 90,100]")
</code></pre>

<p>I would like to arrive at the <em>vecB</em> removing special characters and inserting hyphens, as in the sample generated below:</p>

<pre><code>vecB &lt;- c("0 - 10", "10 - 20", "20 - 30", "50 - 60", "90 - 100")
</code></pre>

<h1>Problem</h1>

<p>I've the <code>gsub</code> syntax, that <em>almost</em> works:</p>

<pre><code>vecB &lt;- gsub(pattern =
                 "^(\\[{1})([[:blank:]]*)(\\d{1,2})([,])(.*)(\\d{2,3})([[:punct:]])$",
                          x = vecA, replacement = "\\3 - \\6")
</code></pre>

<p>The only problem is with the values <code>[ 90,100]</code> which are mistakenly transformed to <strong><code>90 - 00</code></strong> not to <strong><code>90 - 100</code></strong> as it should be (<a href="https://regex101.com/r/rJ7nL3/1" rel="nofollow">regex101</a>).</p>