tags:

views:

121

answers:

5

Hi, I need to check by Regex expression if 9 or 14 digits are typed. The expression "\d{9}|\d{14}" seems to be not working properly, what's wrong ?

A: 
(\d{9}|\d{14}) should work
ennuikiller
A: 

use lenght to check it, regex don't count

Otávio
+1  A: 

It depends on the variant of regular expression syntax you are using as to whether the length {n-m} operator is supported. Which one are you using?

James Keesey
+4  A: 

This regex should work.

^(\d{9}|\d{14})$

Could you post the piece of code you're using and tell us what language are you using? If you're using regex chances are that you have a string, and I'm sure your language has something to count string length.

EDIT:
as Rubens Farias pointed out in comments maybe ^...$ is needed because your regex would match any number with more than 9 digit as that number has a substring with a 9 digit long number.

Anyway check if you can do it with your language's string's methods/functions

Andrea Ambu
maybe you should to use ^..$, to match entire input text.
Rubens Farias
You're right, thanks!
Andrea Ambu
A: 

(\d{9}|\d{14}) should work.

You can also try (\d{9}(?:\d{5})?)

codaddict