Athapali
Athapali

Reputation: 1089

Regex to match three forms of dates

I have dates in these three formats, even though they are all mm/dd/yy

11/12/2014 (\d{2})\/(\d{2})\/(\d{4}) Right matches the date on the left

11/2/2014 (\d{2})\/(\d{1})\/(\d{4}) Right matches the date on the left

5/12/2014 (\d{1})\/(\d{2})\/(\d{4}) This however doesn't match the date on the left, it rather matches the first date, but except the first '1'. so it matches 11/12/2014

I want to match the date pattern where the number before first '/' is always one digit like this: 7/12/2014 (which is July, 12 2014) and is in mm/dd/yy format

Upvotes: 1

Views: 56

Answers (2)

twasbrillig
twasbrillig

Reputation: 18851

(\d{1,2})\/(\d{1,2})\/(\d{4}) matches all 3.

http://regex101.com/r/hT8wS4/1

Update

"I want something to only match the third one.. and not match the first or the second one, even partially."

Use this negative lookahead:

(?<!\d)(\d)\/(\d{2})\/(\d{4})

http://regex101.com/r/hT8wS4/4

This also excludes the form m/d/yyyy as requested, because the day is specified as having to be 2 digits.

Upvotes: 1

kayleeFrye_onDeck
kayleeFrye_onDeck

Reputation: 6958

How about this?

\d{1,2}\/\d{1,2}\/\d{4}

That should get ALL of your form of dates...

If you want it to NOT get double digit months, then use this:

(?<!\d)\d{1}\/\d{1,2}\/\d{4}

To only match the third one:

(?<!\d)\d{1}\/\d{2}\/\d{4}

Upvotes: 1

Related Questions