puser
puser

Reputation: 477

How do you make a regex to select a pattern of X, Y or Z length

If I need to find a regex that matches any positive number of lower case characters, an N, and then either 6 integers, 8 integers or 15 integers. But not match any other number of intagers

e.g. "abcN123456" or "abcdN12345678" or "abN123456789012345" or "abcdefgN123456"

How would you make a regex that finds this?

It starts with [a-z]+N but don't know how to do the variable number of integers

Upvotes: 0

Views: 247

Answers (2)

bukart
bukart

Reputation: 4906

Here's a possible solution

^[a-z]+N(?:\d{6}|\d{8}|\d{15})$

Regular expression visualization

Debuggex Demo

or try this "crazier" variant ;)

^[a-z]+N(?:\d{6}|(?:(?:\d{7}){1,2}\d))$

Regular expression visualization

Debuggex Demo

Upvotes: 2

grexter89
grexter89

Reputation: 1102

This is how I would do it:

[a-z]+N(\d{6}|\d{8}|\d{15})    

Upvotes: 5

Related Questions