regular expression

T

Tony Johansson

Hello!

I need a regular expression pattern for telephonenumber that can have this
format.
The area code can be between 2-4 digits and the number can be between 5-9
digits.
And between we have always a -
So some valid telephonenumbers can look like.
08-123456789
012-12345
0912-12345

So can someone help me with a regular expression for this

//Tony
 
A

Arne Vajhøj

I need a regular expression pattern for telephonenumber that can have
this format.
The area code can be between 2-4 digits and the number can be between
5-9 digits.
And between we have always a -
So some valid telephonenumbers can look like.
08-123456789
012-12345
0912-12345

So can someone help me with a regular expression for this

Try:

\d{2,4}\d{5,9}

Arne
 
B

Brian Cryer

Arne Vajhøj said:
I forgot the dash.

\d{2,4}-\d{5,9}

Nice, but you also forgot the leading zero, so:

0\d{1,3}-\d{5,9}

To the OP:
1. Just a thought, but after the dash can the first digit be a zero?

2. In general, If you need help with regular expressions then my regular
expression glossary might be of some help:
http://www.cryer.co.uk/glossary/r/regular_expression.htm it also contains
links to related resources at the bottom of the page.
 
B

bradbury9

Nice, but you also forgot the leading zero, so:
0\d{1,3}-\d{5,9}

Thats funny, because in Tony's post it did not say that the first digit should be a zero.
1. Just a thought, but after the dash can the first digit be a zero?

If the first digit MUST be a zero, then the regex would be 0\d{1,3}-\d{5,9}
If the first digit CAN be a zero or other number, the first Regex is right \d{2,4}-\d{5,9}
 
A

Arne Vajhøj

Nice, but you also forgot the leading zero, so:

0\d{1,3}-\d{5,9}

I would not say that I forgot, because I did not know
that it was a requirement - nothing in the original post
said that the first digit should be zero.

Arne
 
B

Brian Cryer

Arne Vajhøj said:
I would not say that I forgot, because I did not know
that it was a requirement - nothing in the original post
said that the first digit should be zero.

True. It was an assumption on my part. I think it's a reasonable assumtion
(because area codes certainly in the UK at least always start with a 0), but
it was an assumption. You are right, it wasn't specified as part of the
original requirement. So I was quite wrong to say that you forgot the
leading zero. My apologies.
 
A

Arne Vajhøj

True. It was an assumption on my part. I think it's a reasonable
assumtion (because area codes certainly in the UK at least always start
with a 0), but it was an assumption. You are right, it wasn't specified
as part of the original requirement. So I was quite wrong to say that
you forgot the leading zero. My apologies.

It may be a reasonable assumption if OP is in UK.

But not all countries has such a rule. US does not.
Denmark does not.

A quick googling seems to indicate that Sweden also
use area codes starting with zero, so it may be a good
assumption for OP as well.

Arne
 

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

Top