title: Java Regex to Match a Number Within a Range
body: <p>I'm trying to craft a Java regular expression to identify if a number (which I don't know until execution time) is within a range (and that range I also don't know until execution time).</p>
  
  <p>Specifically, I'm trying to use Cisco's PRIME compliance module to validate my switch has no inactive VLANs (for this question, a VLAN is just a number), but PRIME uses Java regular expression syntax.</p>
  
  <p>I know that the specific switch command I'm evaluating uses a syntax like:</p>
  
  <pre><code>switchport trunk allowed vlan 1,20,37,45,90-101,300-502,904-2044
  </code></pre>
  
  <p>How, then, can I tell if VLAN "x" is in any of those ranges?</p>
  
  <ul>
  <li>If x = 20, it should match.</li>
  <li>If x = 90, it should match.</li>
  <li>If x = 900, it should fail.</li>
  <li>If x = 1043, it should match.</li>
  </ul>
  
  <p>Any ideas?</p>
  
  <p>Edit: Unfortunately, the RegEx listed <a href="https://stackoverflow.com/questions/16721325/regular-expression-two-digit-range-23-79">here</a> is for ranges that are known; the examples are all hard-coded ranges. I need something that takes an unknown x, y, and z, where all x, y, and z might possibly be 1, 2, 3, or 4 digits, and matches if z is between x and y when written as "x-y". </p>
  
  <p>Is there a way to take the string "x-y", parse it into \1 and \2 that are understood to be numbers, and match if (z >= \1 &amp;&amp; z &lt;= \2)?</p>
  
  <p>I've tried looking at things like lookahead and lookbehind and crazy/obscure Java-compatible regex structures, but my head quickly got spun into the 4th dimension.</p>