# Regular Expression Question

**URL:** <https://boards.straightdope.com/t/regular-expression-question/324903>\
**Category:** Factual Questions\
**Created:** [October 5, 2005, 3:58pm UTC](https://boards.straightdope.com/t/regular-expression-question/324903 "2005-10-05T15:58:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ponder\_Stibbons](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponder_stibbons/32/343_2.png) [@Ponder\_Stibbons](https://boards.straightdope.com/u/Ponder_Stibbons)\
**Post date:** [October 5, 2005, 3:58pm UTC](https://boards.straightdope.com/t/regular-expression-question/324903/1 "2005-10-05T15:58:49Z")

</div>

I’ve got this regular expression validator that makes sure a user enters a valid phone number. The expression in question is:

(((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}  
Now that allows entry of US phone numbers in the format (123) 456-7890, 123-456-7890, or 123-4567. However, I want to disallow the 7-digit variety, as getting people’s phone numbers without the area code is a might bit useless, but I can’t quite figure out which bit I need to change. What should this expression be?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 5, 2005, 4:09pm UTC](https://boards.straightdope.com/t/regular-expression-question/324903/2 "2005-10-05T16:09:58Z")

</div>

```auto

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
                        ^
                        |
                        +----- Here

```

The ? indicated by the arrow makes the preceding expression, which matches the area code in either format, optional. If you remove that question mark, it will be required.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 5, 2005, 4:12pm UTC](https://boards.straightdope.com/t/regular-expression-question/324903/3 "2005-10-05T16:12:37Z")

</div>

Assuming d stands for any digit in the range 0-9, and D stands for any digit in the range 2-9 (area codes and exchanges can’t start with 0 or 1), you want something like this:

((Ddd) |Ddd-)Ddd-dddd

Translating this expression into your language’s syntax shouldn’t be too hard. I assume that there’s some escape character for the space, but I don’t know it off the top of my head.

---

<div class="post-metadata">

**Author:** ![Ponder\_Stibbons](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponder_stibbons/32/343_2.png) [@Ponder\_Stibbons](https://boards.straightdope.com/u/Ponder_Stibbons)\
**Post date:** [October 5, 2005, 4:16pm UTC](https://boards.straightdope.com/t/regular-expression-question/324903/4 "2005-10-05T16:16:04Z")

</div>

Thanks, **friedo**. Obviously, I am not a regular expression haxxor. 🙂
