Search code examples
regexcoding-stylestandards

How to represent regex number ranges (e.g. 1 to 12)?


I'm currently using ([1-9]|1[0-2]) to represent inputs from 1 to 12. (Leading zeros not allowed.)

However it seems rather hacky, and on some days it looks outright dirty.

☞ Is there a proper in-built way to do it?

☞ What are some other ways to represent number ranges?


Solution

  • I tend to go with forms like [2-9]|1[0-2]? which avoids backtracking, though it makes little difference here. I've been conditioned by XML Schema to avoid such "ambiguities", even though regex can handle them fine.