As part of a web service response, I'm having to extract date strings that so far have taken the either of these formats:
- 06 Mar 2015-10:24 EST
- 06 Mar 2015-10:24
(I've got no control over the service itself; there's a whole assortment of nonstandard date formats there plus [inaccurate] localization, so you'll have to trust me that in the context I need a regex.)
So far, I've been using the following pattern to pull out the bits and pieces I need:
@"(((\\d{1,2})\\s([a-z]+)\\s(\\d{4}))\\-(\\d+:\\d+))(\\s([a-z]{3}))?$"
However, yet another new format has been introduced, without the time:
- 06 Mar 2015
This seemed like a simple modification. I created a new group around the hyphen+time ("-10:24") atoms, and added the "zero or one" quantifier ("?") to get this:
@"(((\\d{1,2})\\s([a-z]+)\\s(\\d{4}))(\\-(\\d+:\\d+))?)(\\s([a-z]{3}))?$"
But the expression now fails on all of the above input strings.
Interestingly, I've tried replacing the "?" with other quantifiers, and discovered any quantifier that suggests that at least one of those atoms should be present (e.g., (\\-(\\d+:\\d+))+
, (\\-(\\d+:\\d+)){1,2}
) works, whereas those that suggest even the possibility that it might not be there (e.g., (\\-(\\d+:\\d+))*
, (\\-(\\d+:\\d+)){0,1}
) fail.
I can come up with at least a couple of clumsy workarounds for this, but in the interest of clean code, am I…
- Messing up the regular expression? (I don't think so, I've tested this at regex101.com and it works.)
- Missing something in the NSRegularExpression documentation?
- Bumping into an actual bug in the class (in which case I'll go ahead and report it to Apple)?
Thanks.