1

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?

Walters
  • 119
  • 16

1 Answers1

1

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.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • I made a mistake with the question. value2 = "30.Jul.2019" – Walters Jul 26 '19 at 18:58
  • Nck, One more thing – Walters Jul 26 '19 at 19:29
  • I have a next regex where it matches a time. So if there is a successful match with the date, it then reads the same line and checks if there is a match with the time. – Walters Jul 26 '19 at 19:30
  • The same line is: 30.Jul.2019 This the line I want to match 15:04:09 The regex is "^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))?" Could you check if its working properly? – Walters Jul 26 '19 at 19:31
  • 1
    You'd either have to edit your question, post a new one, or go to chat. To help, you'd also need the regex / code you've tried, the result, and the expected behavior. Can't really do that from the comments, and the mods would probably get mad if we did. Let me know what you decide. – Nick Reed Jul 26 '19 at 19:35
  • I upload a new question: https://stackoverflow.com/questions/57226263/c-sharp-regex-to-match-time – Walters Jul 26 '19 at 19:51