-2

Last time I was asked for checking string for minimum 8 digits. And I got following regex:

    /^(?=(.*\d){8,})[\d\(\)\s+-]{8,}$/

You can see the question here: Checking string with minimum 8 digits using regex

Now I want to restrict string to accept maximum 14 digits in same regex. And I tried this:

   /^(?=(.*\d){8,14})[\d\(\)\s+-]{8,}$/

No luck. Please anyone help me in fixing this.

UPDATE

After getting 2 down votes I thought better to write my own. I constructed regex using previous regex. Following regex works for me:

    /^(?=(.*\d){8})(?!(.*\d){15})[\d\(\)\s+-]{8,}$/
Community
  • 1
  • 1
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44
  • 1
    Why would I even bother to reply of you accept 46% of all answers only!? Did you even upvote the 8 digit reply!?!? – akuhn Dec 05 '12 at 07:05
  • Most of the questions I was asked has no acceptable answer. See my 8 digits question here: http://stackoverflow.com/questions/13600290/checking-string-with-minimum-8-digits-using-regex – Rahul Tapali Dec 05 '12 at 07:13
  • why ppl are down voting? Please give the reason before you down vote. – Rahul Tapali Dec 05 '12 at 09:06
  • @akuhn If ppl give answers like answers for this question. How can I accept. Tell me you accept the answers which won't work out to you or incomplete.And don't build unnecessary discussion here. – Rahul Tapali Dec 05 '12 at 09:10
  • 2
    "Please anyone fix this ASAP" does not belong to stackoverflow – kazanaki Dec 05 '12 at 12:15
  • possible duplicate of [Regular expression to limit number of characters to 10](http://stackoverflow.com/questions/1649435/regular-expression-to-limit-number-of-characters-to-10) – Waleed Khan Dec 05 '12 at 14:42
  • @clickit learn how to formulate better questions—to me the answer of tudor-constantin looks like the correct answer to your question, while your update suggests that you are actually looking for something else… – akuhn Dec 05 '12 at 17:42

4 Answers4

5

By your request, the regex should be as simple as:

/^\d{8,14}$/
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
3

From your answer, and your other question, it seems like you are encoding a whole bunch of different rules into one increasingly complicated regex:

  1. the string must be at least 12 chars long
  2. it can only contain digits, parentheses, + and - signs, and spaces
  3. there must be between 8 and 14 digits

While it's possible to do this with a regex, is it worthwhile? I'd argue that such a complicated regex is impossible to read, and therefore difficult to maintain.

If you split up the different criteria, it'll be much clearer.

  1. string.length >= 12
  2. string =~ /^[\d()+-\s]+$/ - note that by using square brackets to create a character class, you don't need to escape things, which also makes it much simpler.
  3. (8..14).include?(string.count("0-9")) - check out the docs for String#count

So, altogether,

def valid?(string)
  string.length >= 12 &&
    string =~ /^[\d()+-\s]+$/ &&
    (8..14).include?(string.count("0-9"))
end

It's a bit longer but it's a heck of a lot more understandable.

Andrew Haines
  • 6,574
  • 21
  • 34
1

Try:

/^(?=(.*\d){8,14}(?!.*\d))[\d\(\)\s+-]{8,}$/

If I got the placement of the negative look ahead right I should fail to match a strimg with more than 14 digits.

TheRuss
  • 318
  • 3
  • 9
0

Try also adding the 14 after the second instance of "8," if the previous regex achieved what you wanted - but it really is more complex than just 8-14 digits!

TheRuss
  • 318
  • 3
  • 9
  • That won't be correct regex. I just want to restrict digits not length of the string. – Rahul Tapali Dec 05 '12 at 06:57
  • Can you update the question with all required conditions? Need to understand the goal. The look ahead (?=) allows "(any char 0 or more times, followed by a digit) 8-14 times. So it doesn't restrict to a 14digit number..... – TheRuss Dec 05 '12 at 07:08
  • Ok I think I see the problem. The match of 14 digits doesn't stop their from being more than 14 - need to add a negative look ahead. I'm gonna put it in a new answer so you can accept it of it works :) – TheRuss Dec 05 '12 at 07:24
  • I written my own and it works: /^(?=(.*\d){8})(?!(.*\d){15})[\d\(\)\s+-]{8,}$/ . Thanks for your time. – Rahul Tapali Dec 05 '12 at 11:28