Sprinkling an ‘or’ on your regex
Had some fun today getting this working. If you need to do a boolean ‘or’ comparison inside a regex with python, this is how I did it:
if (re.match(r”([0-9]{6}|[0-9]{8})$”, mydate)):
In this case, I was trying to either match a date string using 8 digits, YYYYMMDD, or 6 digits, YYMMDD.
Steve Laniel 9:42 am on March 27, 2010 Permalink |
A few things:
1) That’s not just Python. That’s essentially all regular expressions. It’s called “alternation,” if you’re curious. Look in the man page for grep(1).
2) The shorter and better way to say [0-9] is “\d”. So your regex could be shortened to
“\d{6}|\d{7}”.
3) The “character classes” (things like “[0-9]” or “[A-Za-z]“) are actually just more-concise ways of expressing alternation. So, for instance, “[0-9]” is identical to “0|1|2|3|…|8|9″.
4) You may already know that if you were looking for 6, 7, or 8 digits, you could use “\d{6,8}”.