Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The general complaint about regex patterns for phone numbers is that they require one to put in the <em>truly optional</em> characters as dashes and other items. </p> <blockquote> <p>Why can't they be optional and have the pattern <em>not care</em> if they are there or not?</p> </blockquote> <p>The below pattern makes dashes, periods and parenthesis optional for the user and focuses on the numbers as a result using named captures.</p> <p>The pattern is commented (using the <code>#</code> and spans multiple lines) so use the Regex option <code>IgnorePatternWhitespace</code> unless one removes the comments. For that flag doesn't affect regex processing, it only allows for commenting of the pattern via the <code>#</code> character and line break .</p> <pre><code>string pattern = @" ^ # From Beginning of line (?:\(?) # Match but don't capture optional ( (?&lt;AreaCode&gt;\d{3}) # 3 digit area code (?:[\).\s]?) # Optional ) or . or space (?&lt;Prefix&gt;\d{3}) # Prefix (?:[-\.\s]?) # optional - or . or space (?&lt;Suffix&gt;\d{4}) # Suffix (?!\d) # Fail if eleventh number found"; </code></pre> <p>The above pattern just looks for 10 numbers and ignores any filler characters such as a <code>(</code> or a dash <code>-</code> or a space or a tab or even a <code>.</code>. Examples are</p> <pre><code>(555)555-5555 (OK) 5555555555 (ok) 555 555 5555(ok) 555.555.5555 (ok) 55555555556 (not ok - match failure - too many digits) 123.456.789 (failure) </code></pre> <hr> <p><strong>Different Variants of same pattern</strong></p> <p>Pattern without comments no longer need to use <code>IgnorePatternWhiteSpace</code>:</p> <pre><code>^(?:\(?)(?&lt;AreaCode&gt;\d{3})(?:[\).\s]?)(?&lt;Prefix&gt;\d{3})(?:[-\.\s]?)(?&lt;Suffix&gt;\d{4})(?!\d) </code></pre> <p>Pattern when not using Named Captures</p> <pre><code>^(?:\(?)(\d{3})(?:[\).\s]?)(\d{3})(?:[-\.\s]?)(\d{4})(?!\d) </code></pre> <p>Pattern if <code>ExplicitCapture</code> option is used</p> <pre><code>^\(?(?&lt;AreaCode&gt;\d{3})[\).\s]?(?&lt;Prefix&gt;\d{3})[-\.\s](?&lt;Suffix&gt;\d{4})(?!\d) </code></pre>
    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. 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