-1

I'm trying to get a match of 9-digit integer number. The text I'm scanning can be a length of 1-200 character.

The trouble I'm running in to is that I do not want to match if the input has a series of digits longer than 9. I also need to match if the whole input string is 9 digits. Or begins or ends with the 9-digit number.

I've tried:

d{9}      >  This matches sub-strings longer than 9 digits
d{9}(\D)  >  This works unless the sub-string is at the end since this expects some character after the 9 digits.

I have search a lot but I have not found this exact issue. Any ideas?

Note: I happen to be working with ColdFusion for this particular issue but I'm hoping that a general regex will do the trick. If necessary I can code this in CFML.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Chad N.
  • 1
  • 1
  • 1
  • 1
    Should the number be a word by itself, or can letters be around it? If it's a word, use the `\b` word boundary pattern. – Barmar Oct 17 '18 at 22:07
  • There can be anything around it except other digits (or nothing around it). If the input is abd123456789xyz then it is a match. But abd1234567890xyz is not a match because it has the extra digit. Abd sdf 123456789[end of line/file] would match. Thanks! – Chad N. Oct 17 '18 at 22:15

1 Answers1

-1

Use alternatives to match a non-digit or beginning/end of the string:

(^|\D)\d{9}($|\D)
Barmar
  • 741,623
  • 53
  • 500
  • 612