Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I usually don't answer gimme codez questions but hey it's Friday!</p> <p>There are perhaps thousands of questions here on SO following the same pattern:</p> <pre><code>Plz help me with a regular expression for a string that - _must_ contain at least one X - _can_ contain Y </code></pre> <p>and the answer is usually something like</p> <pre><code>/^ Y* X [XY]* $/ </code></pre> <p>or if you're fancy</p> <pre><code>/^ (?=.*X) [XY]+ $ </code></pre> <p>Unfortunately, all these answers (or, rather, these questions) are wrong. The problem, as usual, is that the specs are incorrect - the asker takes some "good" examples and describes them in the question, but doesn't realize that this description also matches many "bad" cases. When taken literally, this question will be answered with an expression that only does a half of its work - yes, it does validate good cases, but it does not reject bad ones. A good expression must do both!</p> <p>Example: I want to validate a telefone number, which is something like <code>123</code> or <code>123-456-789</code>. So I post a question on SO:</p> <pre><code>Plz help me with a regular expression: - must contain at least one digit - can contain a dash </code></pre> <p>and in a few seconds I get</p> <pre><code>/^-*\d[\d-]*$/ </code></pre> <p>which I test with my examples (works!) and insert in my code. On the next morning, to my deepest embarassment, someone registers on my site providing this "telefone number":</p> <pre><code>----------3----------- </code></pre> <p>The moral of the story: never validate "strings". Validate domain objects!</p> <p>To answer this specific question: I can't provide you with a good regular expression until you tell me what it is for.</p>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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