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?

7
  • 1
    Your regex doesn't match either of the samples you provided, according to RegexBuddy. Commented Jul 26, 2019 at 18:06
  • Same with Regex101, no matches. What's this regex trying to do? It seems awfully specific - perhaps there's an easier way to do it. Commented Jul 26, 2019 at 18:07
  • Okay My bad, I made a mistake @NickReed and Ken, value2 is actually equal to "30.Jul.2019" Commented Jul 26, 2019 at 18:57
  • Ah! That DOES return a match, thank you. I'll take a closer look. Commented Jul 26, 2019 at 19:01
  • 1
    @KevoyWalters see edited answer below, looks like the $ is causing an issue. If your regex is preceded by ^ and followed by $, it will only match if the desired expression has no characters before or after it on that line. Commented Jul 26, 2019 at 19:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I made a mistake with the question. value2 = "30.Jul.2019"
Nck, One more thing
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.
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?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.