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.
views:
84answers:
3
+4
A:
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,}'
paxdiablo
2009-10-25 05:00:55
That worked! Turns out though, I was trying it on Emacs, and Emacs regexes are a PITA. Found out you have to escape the curly braces. Thanks for the help :)
obvio171
2009-10-25 05:11:26
Clear explanation. One minor point: at least some systems require the minimum value to be specified (e.g. http://perldoc.perl.org/5.8.8/perlre.html ), so "Where you want unbounded repetitions (to infinity on the high side or zero on the low side), just leave out the number." could be slightly rephrased.
Tim
2009-10-25 08:10:23
Thanks, @Tim. Updated to fix.
paxdiablo
2009-10-25 08:36:14
+1
A:
You can find a tutorial about repetition in Regex (as well as a bunch of other stuff) here
Jason Punyon
2009-10-25 05:02:05