Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regular expression are not the answer to <em>every</em> single problem. My advice would be to do something like:</p> <pre><code>boolean isValidSomethingOrOther (string): if string.length() != 4: return false for each character in string: if not character.isNumeric(): return false if string.toInt() &gt; 1331: return false return true </code></pre> <p>If you <em>must</em> use a regex, there's nothing wrong with your solution but I'd probably use the following variant (just based on my understanding of RE engines and how they work):</p> <pre><code>^0[0-9]{3}|1[0-2][0-9]{2}|13[0-2][0-9]|133[01]$ </code></pre> <ul> <li>The first section matches 0000-0999.</li> <li>The second matches 1000-1299.</li> <li>The third matches 1300-1329.</li> <li>The final one matches 1330 and 1331.</li> </ul> <p><em>Update:</em></p> <p>Just on the elegance comment, there are many forms of elegance of which regexes are one. You can also achieve elegance just by abstracting the validation out to a separate function or macro and then call it from your code:</p> <pre><code>if isValidSomethingOrOther(str) ... </code></pre> <p>where <code>SomethingOrOther</code> is a concrete business object. This allows you to change your idea of a valid object easily, even using a regex as you desire or any other checks you deem appropriate (such as my function above).</p> <p>This allows you to cater for any changes down the line such as the requirement that these object now have to be prime numbers.</p> <p>I'm sure I <em>could</em> write a "prime-number-less-than-1332" regex. I'm equally sure I wouldn't <em>want</em> to - I'd prefer to code that up as a function (or lookup table for raw speed), especially since the regex would most likely just look like:</p> <pre><code>^2|3|5|7| ... |1327$ </code></pre> <p>anyway.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload