-4

How to verify below format using regular expression in c#.. can you help me..

Today's Date: 03:30 AM ET, 02/15/2013

I used below format but its not working..

@"^ Today's Date: (\d{2})(\:)(\d{2})$ AM ET, ^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$"
Vinay Lodha
  • 2,185
  • 20
  • 29
saran
  • 1,273
  • 4
  • 19
  • 34

2 Answers2

0

It looks like there are 3 problems with your regular expression:

  • There is a leading space between ^ and T of Today's
  • the portions of your regular expression for matching the day and the month are in the wrong order
  • The $ and ^ midway through the expression shouldn't be there

(0[1-9]|[12][0-9]|3[01]) matches 01 to 31 and (0[1-9]|1[012]) matches 01 to 12

making the corrected pattern:

^Today's Date: (\d{2})(\:)(\d{2}) AM ET, (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$

But as Rahul R. says in the comments, using a proper date parsing method is probably the way to go.

Community
  • 1
  • 1
mikej
  • 65,295
  • 17
  • 152
  • 131
0

Use this:

^Today's Date: ([01][0-9]|[2[0-3]):([0-5][0-9]) [AP]M ET, ((0[1-9]|[12][0-9]|3[01])/(?=(0[13578]|1[02]))(0[13578]|1[02])|(0[1-9]|[12][0-9]|30)/(?=(0[469]|11))(0[469]|11)|(0[1-9]|[12][0-9])/(?=(02))(02))/(0{3}[1-9]|((?!0{3}\d)\d{4}))$
Naveed S
  • 5,106
  • 4
  • 34
  • 52