9

I need regex to check if a string contains 8 decimal digits or more. It can contain anything else and the digits don't have to be consecutive.

Thanks in advance

EDIT: replaced "number" by "decimal digit" to match accepted answer.

Peter G.
  • 14,786
  • 7
  • 57
  • 75
Alistair
  • 1,939
  • 2
  • 22
  • 31

5 Answers5

18
/([^\d]*\d){8}/

Perhaps not the most elegant / efficient way to do it, but it works. Basically it will match eight decimals (optionally, with non-decimals between them). If there are more than eight, it will match too.

EDIT
As @Tomalak has pointed out, [^\d] equals \D by definition:

/(\D*\d){8}/
jensgram
  • 31,109
  • 6
  • 81
  • 98
5

A tweak to allow the last character to be non-numeric:

/(\D*\d){8,}\D*/
chaserino
  • 154
  • 2
  • 4
2
/\d{8,}/

Will match the 8 or more decimals requested by original poster

cjac
  • 386
  • 2
  • 5
2
/(?:\d+\D+){7,}\d+/

This will match at least 8 numbers with something other than numbers in between.

eisberg
  • 3,731
  • 2
  • 27
  • 38
0
/(?:(.+)?\d+(.+)?){8}/

Something like this?

Oliver O'Neill
  • 1,229
  • 6
  • 11