Question

How do I define a minimum and maximum (possibly unbounded) number of times a certain pattern should repeat itself? I know there's ? and *, with which I could build the pattern by repeating it a certain amount of times, but I know there's a special notation for it using {}, I just can't remember how it is.

Was it helpful?

Solution

For a minimum of m and maximum of n, you use {m,n}. If m and n are the same, just use {m}.

For example, a line consisting only of three to four alphas followed by two numerics followed by six to twelve alphanumerics would be:

^[A-Za-z]{3,4}[0-9]{2}[A-Za-z0-9]{6,12}$

Where you want unbounded repetitions on the high side (no maximum number), just leave out the n. For unbounded repetitions on the low side, there are some implementations that don't support leaving out the m so you may want to just specify 0 for that to be safe). In other words,

[a-z]{6,}[0-9]{0,4}

means six or more lowercase letters followed by zero to four digits.

Your special cases are just versions of that, as in:

'[a-z]?' is identical to '[a-z]{0,1}'
'[a-z]*'                 '[a-z]{0,}'
'[a-z]+'                 '[a-z]{1,}'

OTHER TIPS

After the pattern include {min,max}

You can find a tutorial about repetition in Regex (as well as a bunch of other stuff) here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top