Fabian Jost
Fabian Jost

Reputation: 21

Matching string with regex containing colon

I try to match different date formats that I parse from different files. One date format is for example: "Thu, 12 Nov 2009 14:17:44 -0000"

When I try to match this string with the first regex I get a true result, when I use the second one I get false.

[A-Za-z]{3},\\s+\\d{2}\\s+[A-Za-z]{3}\\s+\\d{2}.*   (1. regex)
[A-Za-z]{3},\\s+\\d{2}\\s+[A-Za-z]{3}\\s+\\d{2}:.*  (2. regex)

In my opinion both regex should match the above date format. Where is my failure?

Upvotes: 1

Views: 600

Answers (3)

cl-r
cl-r

Reputation: 1264

You have forgotten the year :

"[A-Za-z]{3},\\s+\\d{2}\\s+[A-Za-z]{3} \\p{Digit}{4} (\\p{Digit}{2}:){2}[0-9][0-9].*";

You can also test hour in different format.

Upvotes: 0

Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4182

I suspect that the last part "\s+\d{2}:.*" was to match the "14:", but you have forgotten the year. So the parser expects a ":" but finds the 2nd zero in 2009.

What you need is something like this:

[A-Za-z]{3},\\s+\\d{2}\\s+[A-Za-z]{3}\\s+\\d{4}\\s+\\d{2}:.*  (2. regex)

(I think that should pass :))

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76888

You seem to be forgetting the 4 digit year, therefore the second regex isn't corrrect.

The final part of your first regex is matching the first two digits of 2009 and then everything after.

Upvotes: 1

Related Questions