1

I want to implement the following validation. Match at least 5 digits and also some other characters between(for example letters and slashes). For example 12345, 1A/2345, B22226, 21113C are all valid combinations. But 1234, AA1234 are not. I know that {5,} gives minimum number of occurrences, but I don't know how to cope with the other characters. I mean [0-9A-Z/]{5,} won't work:(. I just don't know where to put the other characters in the regex expression.

Thanks in advance!

Best regards, Petar

5
  • Why is AA1234 invalid? Commented Feb 21, 2011 at 8:17
  • @deceze - It doesn't have 5 digits:) Commented Feb 21, 2011 at 8:18
  • So the rule is "5 digits + any number of letters and slashes"? Commented Feb 21, 2011 at 8:19
  • @deceze - Yes, the number of letters and slashes can be >= 0. Commented Feb 21, 2011 at 8:19
  • 1
    The rule is "at least 5 digits" according to the text. Commented Feb 21, 2011 at 8:23

3 Answers 3

4

Using the simplest regex features since you haven't specified which engine you're using, you can try:

    .*([0-9].*){5}
    |/|\   /|/| |
    | | \ / | | +--> exactly five occurrences of the group
    | |  |  | +----> end group
    | |  |  +------> zero or more of any character
    | |  +---------> any digit
    | +------------> begin group  
    +--------------> zero or more of any character

This gives you any number (including zero) of characters, followed by a group consisting of a single digit and any number of characters again. That group is repeated exactly five times.

That'll match any string with five or more digits in it, along with anything else.

If you want to limit what the other characters can be, use something other than .. For example, alphas only would be:

[A-Za-z]*([0-9][A-Za-z]*){5}
Sign up to request clarification or add additional context in comments.

4 Comments

What if I want to specify which are the other characters?
@Petar, then you would use a different pattern from . - see the update.
Thank you! I am just also curious how will the regex look like, if the following requirement is added - put an upper bound of the number of other characters.
@Petar, don't use regexes for everything. Once they start getting too complicated and unreadable in less than a second, you should be looking at more procedural solutions. For example, ^.{15}$ will give you a 15-character string but so will if (len(str) == 15). That's a basic example but you're entering territory where you're better off just counting characters (digit and non-digit) and just checking those two values.
3

EDIT: I'm picking up your suggestion from a comment to paxdiablo's answer: This regex now implements an upper bound of five for the number of "other" characters:

^(?=(?:[A-Z/]*\d){5})(?!(?:\d*[A-Z/]){6})[\dA-Z/]*$

will match and return a string that has at least five digits and zero or more of the "other" allowed characters A-Z or /. No other characters are allowed.

Explanation:

^          # Start of string
(?=        # Assert that it's possible to match the following:
 (?:       # Match this group:
  [A-Z/]*  # zero or more non-digits, but allowed characters
  \d       # exactly one digit
 ){5}      # five times
)          # End of lookahead assertion.
(?!        # Now assert that it's impossible to match the following:
 (?:       # Match this group:
  \d*      # zero or more digits
  [A-Z/]   # exactly one "other" character
 ){6}      # six times (change this number to "upper bound + 1")
)          # End of assertion.
[\dA-Z/]*  # Now match the actual string, allowing only these characters.
$          # Anchor the match at the end of the string.

4 Comments

The number of other characters can be 0.
Ah, OK. Well that makes the regex even simpler.
Even if it's not a perfect solution +1 for clear explanation.
@Petar: Updated the regex to implement an upper bound for "other" characters.
2

You may want to try counting the digits instead. I feel its much cleaner than writing a complex regex.

>> "ABC12345".gsub(/[^0-9]/,"").size >= 5
=> true

the above says substitute all things not numbers, and then finding the length of those remaining. You can do the same thing using your own choice of language. The most fundamental way would be to iterate the string you have, counting each character which is a digit until it reaches 5 (or not) and doing stuff accordingly.

4 Comments

The point is that this is a part of complex application where all validations are regex. So I must use regex.
@Johnsyweb, note I said complex regex. I know I am using a regex. But my approach is much simpler to understand and cleaner to read.
Sorry, I should have prefixed my comment with @Peter, since this was in response to his comment.
@Johnsyweb - I should have phrased my comment more clearly. What I wanted to say is that the regexes are used in a true, false manner. I mean either it matches or not. I can't use such custom logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.