Reputation: 28760
I have the following regex [0-9-]{4}-[0-9-]{2}-[0-9-]{2}$
.
I want it not to match when it is an iso date:
e.g. 1970-01-01
But I would like it to match when it is like 1979------
or --------29
.
or -----11---
.
If I was using just javascript I would say str.indexOf('--') !== -1
Upvotes: 0
Views: 46
Reputation: 30985
You can use a trick with a negative lookahead like this:
(?!\d{4}-\d{2}-\d{2})^[0-9-]{4}-[0-9-]{2}-[0-9-]{2}$
Btw, you can shorten your regex like this:
(?!\d{4}-\d{2}-\d{2})^[\d-]{4}-[\d-]{2}-[\d-]{2}$
Update: as ctwheels mentioned in his comment, you can make the regex more efficient by using anchors in the lookarounds like this:
^(?!\d{4}-\d{2}-\d{2})[0-9-]{4}-[0-9-]{2}-[0-9-]{2}$
Upvotes: 0
Reputation: 520878
You might find the following pattern satisfactory:
^(?=.*\-\-)(?=.*[0-9]{2})(?:[0-9]{4}|\-{4})-(?:[0-9]{2}|\-{2})-(?:[0-9]{2}|\-{2})$
The first lookahead at the start of the pattern asserts that --
appears at least once, ensuring that the date is not a complete ISO date. The second lookahead asserts that at least one date component is present, preventing ----------
from being a match.
Upvotes: 1