OLIVER.KOO
OLIVER.KOO

Reputation: 5993

Regex - Can quantifier skip certain range?

I have a simple regex like this [0-9a-zA-Z]{32,45} that matches 0-9,a-z,A-Z 32 to 45 times. Is there a way I can have the regex skip a certain range? For example, I don't want to match if there are 40 characters.

Upvotes: 3

Views: 181

Answers (2)

The fourth bird
The fourth bird

Reputation: 163217

Another way could be using an alternation | repeating the character class either 41-45 times or 32-39 times.

You could prepend and append a word boundary \b to the pattern.

\b(?:[0-9a-zA-Z]{41,45}|[0-9a-zA-Z]{32,39})\b

Regex demo

Upvotes: 2

Ryszard Czech
Ryszard Czech

Reputation: 18611

One way to do that would be

\b[0-9a-zA-Z]{32,39}+(?:[0-9a-zA-Z]{2,6})?\b

See proof. You match 32 to 39 occurrences possessively, then an optional occurrence of 2 to 6 repetitions of the pattern.

Upvotes: 2

Related Questions