Best Regular Expression for a one or two values optionally divided by a certain character I'm looking for the most concise regex that matches one or two 4-digit years in any the following setups:


year 
year-
-year
year-year


I can't think of anything slicker than this:

[\\-]?\d{4}|\d{4}\[\\-](\d{4})?
This is a somewhat difficult one because regular expressions inherently lack memory, so you can't tell on the back whether the front existed, so I don't think one can get better than the one you wrote for that particular set.  If you wanted to allow some sort of variant, you could potentially find a better one.