Matching any certain selection of inputs I have the following RegEx which matches any of a selection of inputs (see the list below).

While it works fine, I'm wondering if there is a better way of writing it, as it looks a little dirty.  I've been using various RegEx builders but couldn't come up with a shorter version, so any tips would be appreciated.

-[0-9]+px$|^[0-9]+px$|^[0-9]+em$|^-[0-9]+em$|^[0-9]+\%$|^auto$|^0$


Here is the list of valid entries (where 10 can be any integer):


10px
-10px
10em
-10em
10%
auto
0
You can combine px and em into a single group
You can pull in the negation sign with a ?
[0-9] is equivalent (usually) to \d
^ and $ can be pulled out to the end (or use the api than only matches the entire string.


this results in:

^(-?\d+(px|em)|\d+%|auto|0)$


If you can have a negative % values then it's even simpler: ^(-?\d+(px|em|%)|auto|0)$