Celeb Glow
general | February 28, 2026

Regex ignore match if contained within 23 digit string

I'm having a problem coming with a solution to a situation, I have this regex to match an specific digit sequence:

(102030)\d{10}

Using this as a test data, i get the following matches:

75277887243040354513001
55345377243014107206375
810203087654135168 <- Match
1102030698798798477 <- Match
71020307248040361799581 <- Match

What I'm trying to do is ignore cases where there are 23 digits on the string, is that possible using only regex? I've tried with negative look ahead but haven't been able to achieve it.

2 Answers

For look ahead you need something before it - so let start with

\d

Then by negative ahead of the next 22 digits:

(?!\d{22})

we filter out all lines with 23 digits. Then may follow your original string

(102030)\d{10}

So the full regular expression will be

\d(?!\d{22})(102030)\d{10}

See the result at regex101.com:

enter image description here

\b(?!\d{23}\b)\d*((102030)\d{10})\d*\b

This captures the match in its first subgroup.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy