Home > Back-end >  REGEX find 6 consecutive numbers in a string, but ignore more than 6 consecutive number groups
REGEX find 6 consecutive numbers in a string, but ignore more than 6 consecutive number groups

Time:10-18

I need to REGEX find 6 consecutive digits in a string[

I need to find exactly 6. ignore sequences if they have more or less than 6

abc12345 - no match, less than 6 consecutive digits

abc123456 - i need to match only this string and return 123456

abc1234567 - no match, more than 6 consecutive digits

abc12345678 - no match, morethan 6 consecutive digits

this: \d{6} will match first 6 but it will get first 6 from abc12345678 too, which i need to ignore.

CodePudding user response:

If you re engine supports lookarounds, you can do:

(?<=\D|^)(\d{6})(?=\D|$)

Demo

Alternatively with negative lookarounds:

(?<!\d)(\d{6})(?!\d)

Demo

  • Related