Regular expression to match phone number?
Asked Answered
H

6

3

I want to match a phone number that can have letters and an optional hyphen:

  • This is valid: 333-WELL
  • This is also valid: 4URGENT

In other words, there can be at most one hyphen but if there is no hyphen, there can be at most seven 0-9 or A-Z characters.

I dont know how to do and "if statement" in a regex. Is that even possible?

Heiskell answered 15/11, 2009 at 5:28 Comment(4)
An answer has been accepted, but I'd like to know: Can that hyphen be anywhere in the number, or must it always be (if at all) between the 3rd and 4th digit?Chema
Is there a reason that you need to use a regex?Exalted
@Carl Smotricz: it can be anywhere. good question.Heiskell
@voyager: I'm writing the XSD restriction pattern facet. So yes, I need to use regex.Heiskell
P
4

You seek the alternation operator, indicated with pipe character: |

However, you may need either 7 alternatives (1 for each hyphen location + 1 for no hyphen), or you may require the hyphen between 3rd and 4th character and use 2 alternatives.

One use of alternation operator defines two alternatives, as in:

({3,3}[0-9A-Za-z]-{4,4}[0-9A-Za-z]|{7,7}[0-9A-Za-z])
Prairial answered 15/11, 2009 at 5:29 Comment(0)
S
5

I think this should do it:

/^[a-zA-Z0-9]{3}-?[a-zA-Z0-9]{4}$/

It matches 3 letters or numbers followed by an optional hyphen followed by 4 letters or numbers. This one works in ruby. Depending on the regex engine you're using you may need to alter it slightly.

Serdab answered 15/11, 2009 at 5:36 Comment(4)
i have one question..so i tested the above regex, and it works fine. however, when i add more digits to the end, the regex validator still says it's valid. why is that? shouldnt the expression become invalid as soon as i add 1 more character? i test at tools.netshiftmedia.com/regexlibrary using 121333333Heiskell
@ShaChris23 because 121333333 also matches it at the beginning. For true full length matching, it'd be /^[a-zA-Z0-9]{3}-?[a-zA-Z0-9]{4}$/ (use the ^ and $ symbols to denote beginning of string and end of string (respectively))Postexilian
thank you, Dave! wow..I feel like I learned quite a bit from this one example. (^_^)Heiskell
This is a really good answer. In some ways, it is better than my (accepted) answer.Prairial
P
4

You seek the alternation operator, indicated with pipe character: |

However, you may need either 7 alternatives (1 for each hyphen location + 1 for no hyphen), or you may require the hyphen between 3rd and 4th character and use 2 alternatives.

One use of alternation operator defines two alternatives, as in:

({3,3}[0-9A-Za-z]-{4,4}[0-9A-Za-z]|{7,7}[0-9A-Za-z])
Prairial answered 15/11, 2009 at 5:29 Comment(0)
R
1

Not sure if this counts, but I'd break it into two regexes:

#!/usr/bin/perl

use strict;
use warnings;

my $text = '333-URGE';

print "Format OK\n" if $text =~ m/^[\dA-Z]{1,6}-?[\dA-Z]{1,6}$/;
print "Length OK\n" if $text =~ m/^(?:[\dA-Z]{7}|[\dA-Z-]{8})$/;

This should avoid accepting multiple dashes, dashes in the wrong place, etc...

Rootstock answered 15/11, 2009 at 5:41 Comment(0)
W
1

Supposing that you want to allow the hyphen to be anywhere, lookarounds will be of use to you. Something like this:

^([A-Z0-9]{7}|(?=^[^-]+-[^-]+$)[A-Z0-9-]{8})$

There are two main parts to this pattern: [A-Z0-9]{7} to match a hyphen-free string and (?=^[^-]+-[^-]+$)[A-Z0-9-]{8} to match a hyphenated string.

The (?=^[^-]+-[^-]+$) will match for any string with a SINGLE hyphen in it (and the hyphen isn't the first or last character), then the [A-Z0-9-]{8} part will count the characters and make sure they are all valid.

Wanderoo answered 15/11, 2009 at 5:45 Comment(1)
Note: I revised my answer after testing the regex.Wanderoo
H
0

Thank you Heath Hunnicutt for his alternation operator answer as well as showing me an example.

Based on his advice, here's my answer:

[A-Z0-9]{7}|[A-Z0-9][A-Z0-9-]{7}

Note: I tested my regex here. (Just including this for reference)

Heiskell answered 15/11, 2009 at 5:34 Comment(1)
This will consider 0------ to be valid.Eisenstein
C
0

I use a or with two patterns 7 alpha numeric characters or 3 alpha numeric character a hyphen and 4 alpha numeric characters

def check_string(string):
    if re.match(r'^[A-Z0-9]{7}$|^[A-Z0-9]{3}-[A-Z0-9]{4}$', string):
        return True
    else:
        return False
Cooney answered 23/3, 2023 at 15:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.