Need Regex for phone number

M

Martijn Lievaart

I need Regex to indentify 3108222400 phone number.

/3108222400/ should work. If that is not what you mean, please better
identify what you want matched.

M4
 
K

Keith Thompson

lotug said:
I need Regex to indentify 3108222400 phone number.

my $num = "3108222400";
if ($num =~ /^3108222400$/) {
print "Yes, it's a phone number\n";
}

Now what did you really want to ask? Do you need to recognize
international numbers, or just numbers within the NANP? Is the area
code required? Can the area code be surrounded by parentheses? Can
you have spaces between digits? Hyphens? Periods? Can non-digit
characters, if any, be placed arbitrarily, or must they reflect
the structure of the number? Is a leading "1" allowed? What about
"+1"? What about extensions? What about textual representations
like 1-800-FLOWERS?
 
U

Uri Guttman

KT> my $num = "3108222400";
KT> if ($num =~ /^3108222400$/) {
KT> print "Yes, it's a phone number\n";
KT> }

KT> Now what did you really want to ask? Do you need to recognize
KT> international numbers, or just numbers within the NANP? Is the area
KT> code required? Can the area code be surrounded by parentheses? Can
KT> you have spaces between digits? Hyphens? Periods? Can non-digit
KT> characters, if any, be placed arbitrarily, or must they reflect
KT> the structure of the number? Is a leading "1" allowed? What about
KT> "+1"? What about extensions? What about textual representations
KT> like 1-800-FLOWERS?

if ( m{\QNow what did you really want to ask? Do you need to recognize
international numbers, or just numbers within the NANP? Is the area
code required? Can the area code be surrounded by parentheses? Can
you have spaces between digits? Hyphens? Periods? Can non-digit
characters, if any, be placed arbitrarily, or must they reflect
the structure of the number? Is a leading "1" allowed? What about
"+1"? What about extensions? What about textual representations
like 1-800-FLOWERS?} )

print "Yes, it has phone number like attributes\n" ;

:)

uri
 
R

Randal L. Schwartz

lotug> I need Regex to indentify 3108222400 phone number.

How far do you want to indent it?

s/^/ /;

will insert four spaces.

:)
 
T

Tomasz Chmielewski

On 23.09.2010 23:16, Uri Guttman wrote:

(...)
KT> Now what did you really want to ask? Do you need to recognize
KT> international numbers, or just numbers within the NANP? Is the area
KT> code required? Can the area code be surrounded by parentheses? Can
KT> you have spaces between digits? Hyphens? Periods? Can non-digit
KT> characters, if any, be placed arbitrarily, or must they reflect
KT> the structure of the number? Is a leading "1" allowed? What about
KT> "+1"? What about extensions? What about textual representations
KT> like 1-800-FLOWERS?

if ( m{\QNow what did you really want to ask? Do you need to recognize
international numbers, or just numbers within the NANP? Is the area
code required? Can the area code be surrounded by parentheses? Can
you have spaces between digits? Hyphens? Periods? Can non-digit
characters, if any, be placed arbitrarily, or must they reflect
the structure of the number? Is a leading "1" allowed? What about
"+1"? What about extensions? What about textual representations
like 1-800-FLOWERS?} )

print "Yes, it has phone number like attributes\n" ;

:)

Ah, you forgot:

use Phone::Regex qw(3108222400 flowers);

;)
 
L

lotug

You could try:
if ($_ =~ /3108222400/) {
   .
   .
   .

}

or did you have something else in mind?

   Justin.

Sorry, I guess I assumed to much.
Yes, I was looking for regex that would identify the phone number in a
txt string regardless of how it was formatted. Different people input
phone numbers in different ways. Also, I have regex that identifies a
phone number in a string, but I'm looking for perl regex that will
identify this particular number within a text string.

3108222400
(310) 822-2400
1(310) 822-2400
1-310-822-2400

Etc., etc.

Thanks for all of the responses.
 
T

Ted Zlatanov

l> Yes, I was looking for regex that would identify the phone number in a
l> txt string regardless of how it was formatted. Different people input
l> phone numbers in different ways. Also, I have regex that identifies a
l> phone number in a string, but I'm looking for perl regex that will
l> identify this particular number within a text string.

l> 3108222400
l> (310) 822-2400
l> 1(310) 822-2400
l> 1-310-822-2400

(assuming you are planning to extend this to multiple phone numbers)

You may want to extract the phone number and convert it into an integer
(so the integer is your normalized representation). Then compare that
integer to the constant 3108222400. This strategy has the advantage
that storing many integers is easier and cheaper than the equivalent
strings and that integer comparison is faster. It will only work for
US-and-Canada-style phone numbers.

So here's one simple way to do it; it's not optimized.

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

foreach my $input (<DATA>)
{
chomp $input;
my $original = $input;
$input =~ s/\D//g; # remove all non-digits

foreach my $sought (13108222400) # add more test numbers here
{
my $test = $input;
# prepend the 1 if necessary (so all empty or invalid strings are 1 too)
$test = "1$test" if length $input != length $sought;

print "$original produced normalized $test\n";
print "yes, $test == $sought\n" if $test == $sought;
}
}

__DATA__
3108222400
(310) 822-2400
1(310) 822-2400
1-310-822-2400
nonsense
3108222401
310822240
1310822240

1nonsense3108222400
 
C

Charlton Wilbur

lotug> I need Regex to indentify 3108222400 phone number.

One of my favorite job interview questions, as an interviewer, goes
something like this:

We have a text-input field that we ask the user to type a
telephone number into. As part of data validation, we need to
know if it's a valid phone number and to identify the area
code. Please write some code to do that.

Now, yes, in the real world this is a hard problem, but I want to see
how the candidate thinks. Within 10 minutes after asking this question
and handing the candidate the whiteboard marker, I have a solid
impression.

(And some of that impression comes from the questions the candidate
asks, because the specification is vague. "US phone numbers only?" is a
very good question. "You must do this a lot - do you have a library
for it already?" is another very good question.)

It's almost embarrassing how many times I've had to stop a candidate
after 20 minutes of hinting and nudging and say, "Okay, there are
several ways to do this, but here's the regex solution" just so we can
move on with the interview - but at that point it's pretty much over
anyway.

Charlton
 
R

Randal L. Schwartz

Charlton> Within 10 minutes after asking this question
Charlton> and handing the candidate the whiteboard marker, I have a solid
Charlton> impression.

The whiteboard marker without the whiteboard? Is the interview over if
the candidate just starts sniffing it? :)
 
S

sln

lotug> I need Regex to indentify 3108222400 phone number.

One of my favorite job interview questions, as an interviewer, goes
something like this:

We have a text-input field that we ask the user to type a
telephone number into. As part of data validation, we need to
know if it's a valid phone number and to identify the area
code. Please write some code to do that.

Now, yes, in the real world this is a hard problem, but I want to see
how the candidate thinks. Within 10 minutes after asking this question
and handing the candidate the whiteboard marker, I have a solid
impression.

(And some of that impression comes from the questions the candidate
asks, because the specification is vague. "US phone numbers only?" is a
very good question. "You must do this a lot - do you have a library
for it already?" is another very good question.)

It's almost embarrassing how many times I've had to stop a candidate
after 20 minutes of hinting and nudging and say, "Okay, there are
several ways to do this, but here's the regex solution" just so we can
move on with the interview - but at that point it's pretty much over
anyway.

Given the simplicity of the Perl part, this would favor a phone
number expert.

-sln
 
C

Charlton Wilbur

sln> Given the simplicity of the Perl part, this would favor a phone
sln> number expert.

You'd think that - but in practice, there are a lot of people who
present themselves as Perl experts convincingly enough to get past HR
people and generalist recruiters. It is quite simple; a candidate who's
got solid Perl skills can answer this in five minutes, and then you can
ask other questions like "How would you solve this if you couldn't
capture things from the m// operator?" Someone who's faking it will
bluster and bumble until the interviewer steps in to answer it.

Charlton
 
C

Charlton Wilbur

Charlton> Within 10 minutes after asking this question and handing
Charlton> the candidate the whiteboard marker, I have a solid
Charlton> impression.

RLS> The whiteboard marker without the whiteboard? Is the interview
RLS> over if the candidate just starts sniffing it? :)

No -- but it's a sign of a potential good fit with our group's culture.

Charlton
 
B

Bart Lateur

Charlton said:
We have a text-input field that we ask the user to type a
telephone number into. As part of data validation, we need to
know if it's a valid phone number and to identify the area
code. Please write some code to do that.

The only way to really be sure is to call up the number.

Yes, it is indeed similar to the problem of checking if an email address
is valid.
 
M

Marc Girod

You'd think that - but in practice, there are a lot of people who
present themselves as Perl experts convincingly enough to get past HR
people and generalist recruiters.

This kind of interview strategy does select some people and reject
others.

Whether the people selected are more suitable to the job than the
people rejected is questionable. It is only obvious to certain kinds
of people. It is not to others.

To make it simple, my theory is that there are people who like
questions and people who like answers.
People who like answers believe that people who like answers are
better.
There is no way on earth to contradict them.

I tend to get rejected in such tests. It doesn't seem to depend on the
area of expertise.
I have also been in situation to test people. I did select somebody.

Marc
 
C

Charlton Wilbur

MG> This kind of interview strategy does select some people and
MG> reject others.

That's exactly the point.

MG> Whether the people selected are more suitable to the job than
MG> the people rejected is questionable. It is only obvious to
MG> certain kinds of people. It is not to others.

If you don't have basic Perl knowledge -- and a level of regular
expressions knowledge sufficient to make at least a reasonable attempt
at solving that problem -- then you are unsuited to a position where you
will be expected to program in Perl. Since programming in Perl is a
primary requirement for the positions I'm interviewing candidates for,
assessing whether or not they have a working knowledge of basic to
intermediate Perl is one of my primary goals in the interview.

Charlton
 
T

Ted Zlatanov

CW> You'd think that - but in practice, there are a lot of people who
CW> present themselves as Perl experts convincingly enough to get past HR
CW> people and generalist recruiters.

"Would you describe yourself as a scalar, list, or hash and why?"

"If you could be any CPAN module, which would you choose and why?"

"How does Perl 6 make you feel?"

"Do you play golf?"

Ted
 
M

Marc Girod

If you don't have basic Perl knowledge

But that's not what you measure, or determine, or show.
I repeat myself:
I tend to get rejected in such tests. It doesn't seem to depend on
the
area of expertise.

Do you mean that I am worth *nothing*?
Yes. I can see you do.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top