2

There are topics on Stack Overflow on how to match a number of a certain length, but that's not what I'm trying to do.

I'd like to match a number that has 5-digits or more but not if it's followed or preceded by anything other than numbers.

Community
  • 1
  • 1
M.K. Safi
  • 6,560
  • 10
  • 40
  • 58

4 Answers4

12

You can use \d{5,}, which matches 5 digits or more, then:

  • If you want this number as a word use \b\d{5,}\b. \b matches word boundaries.
  • If that’s a number on its own line, use ^\d{5,}$. ^ matches the beginning of the line while $ matches its end.

Here is an example.

bfontaine
  • 18,169
  • 13
  • 73
  • 107
3

You may try this regex:

^\D*(?:\d\D*){5,}$

REGEX DEMO

or more simple:

^\d{5,}$

REGEX DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Use ^ for the start of string and $ for the end of string.

/^[\d]{5,}$/

So string of '12354' is true but string of '12345 foo' is false. Is it your question?

doubleui
  • 516
  • 2
  • 8
0

I hope this is what you're looking for

/(?<=\d\s)\d{5,}(?=\s\d)/g

Explanation:

(?<=                     look behind to see if there is:
  \d                       digits (0-9)
  \s                       whitespace (\n, \r, \t, \f, and " ")
)                        end of look-behind
\d{5,}                   digits (0-9) (at least 5 times)
(?=                      look ahead to see if there is:
  \s                       whitespace (\n, \r, \t, \f, and " ")
  \d                       digits (0-9)
)                        end of look-ahead

Demo

Or something more complicated

/(?<=\d(?:\s))\d{5,}(?=\s(?:\d))/g
hex494D49
  • 9,109
  • 3
  • 38
  • 47