Walters
Walters

Reputation: 131

C# Regular Expression not matching

I have a regular expression

 string dateformattwo = @"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})";

and two strings

string value = "30.Jul.2019 This is the line that I want to match"
string value2 = "30.jul.2019"

The regex is correct however it does not match with value but it matches with value2. Why is that happening?

Upvotes: 1

Views: 283

Answers (1)

Nick Reed
Nick Reed

Reputation: 5059

I couldn't get your regex to match your strings, so it's hard to say exactly what's expected here, but I can take a guess as to why it's not working: nowhere in your regex are you looking for july - looks to me like you're only matching for JUL.

Edit: each of your regexes end with $, which asserts its position at the end of the line. Your first line fails because there's characters after the date.

Updates regex here which, despite being a php-matching regex as pointed out in the comments, still matches your desired text.

Upvotes: 1

Related Questions