Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 743
Pacerier Avatar asked Aug 18 '11 09:08

Pacerier


1 Answers

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.

like image 86
xan Avatar answered Nov 03 '22 09:11

xan