2

Example: there must be at least two digits in a given string independent of their position.

This will only work IF THEY ARE TOGETHER: /[0-9]{2}/

If they are separated like a1b2 it doesn't work.

EDIT: Thanks for the responses, but I realise this example was too trivial. Try this instead: there must be at least two digits in a given string AND two of the letter x AND two of the character & AND two of the letter J.

Wouldn't adding .* between every possible permutation get really lengthy? is there no way to not be concerned about the order?

Simon Lang
  • 40,171
  • 9
  • 49
  • 58
  • 1
    possible duplicate of [regex: contains at least 8 decimal digits](http://stackoverflow.com/questions/5416250/regex-contains-at-least-8-decimal-digits) – Felix Kling Nov 01 '11 at 14:33
  • `[0-9]{2}` will match two adjacent digits - so just test for that match. Am I missing something? – El Ronnoco Nov 01 '11 at 14:33
  • 1
    Maybe this gives you inspiration: http://stackoverflow.com/questions/5950756/regex-for-checking-that-at-least-3-of-4-different-character-groups-exist – Felix Kling Nov 01 '11 at 14:41
  • @FelixKling thanks that is the best point in the right direction I've got so far. So, basically it looks like I'm going to have a super-huge ugly regex. Or, split it into multiple regexes and test them all – Simon Lang Nov 01 '11 at 14:44

3 Answers3

3

The following will work for you:

/\d.*\d/
2

"Thanks for the responses, but this was supposed to be a trivial example. What if it were more like"

Well yes since you have more rules for your regex it will get more lengthy. This is inevitable. In general when you want to check if there is at least something you should use positive lookahead assertions :

/^(?=.*\d.*\d)(?=.*x.*x).*$/

This account for at least two digits and at least two x's. The rest I leave it to you.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
1

You can just say a digit followed by anything (or nothing) and then a digit: /[0-9].*[0-9]/

Benjie
  • 7,701
  • 5
  • 29
  • 44