0

I want to check if a string contains digits in the format dddd-dd-dd. I'm not interested in checking valid dates.

Examples:

  • Input: att_token_2015-09-02

    Expected output: True

  • Input: att_token_1234-99-99

    Expected output: True

  • Input: att_token_1234

    Expected output: False

EDIT: I haven't tried this in any program. I just want the regex pattern for it.

(\d+)-(\d+)-(\d+)

This is what I have tried so far and it returns me the numbers but I do not know if this is right.

16
  • 1
    Post your attempts to achieve this. Commented Sep 2, 2015 at 11:59
  • 1
    What is your question? What language are you using? Please read How to ask a good question. Commented Sep 2, 2015 at 12:00
  • 1
    The regex you have is good to go. Just check with date_parse if the return value is not FALSE, and you are done (this is for PHP, you need to use one for your programming language). And the reason for downvotes is simple: validating datetime values is veeery frequent issue on SO, you should really search a bit, and you will find tons of solutions. Commented Sep 2, 2015 at 12:10
  • 2
    The question isn't unclear, Stack Overflow is just full of people that would rather downvote an easy question because you could have looked harder for an answer than to just answer the question. Commented Sep 2, 2015 at 12:11
  • 1
    Another question weak point: no programming language is specified. That is not how a regex question should be asked, and it is clearly written in the regex tag description. Commented Sep 2, 2015 at 12:18

1 Answer 1

1

\d{4}-\d{2}-\d{2} will match digits in the yyyy-mm-dd pattern, but it won't match dates - it would, for instance, match 1234-99-99. You can access the match in whatever language you are using and try to do date parsing on the match to see if it's a date.

You were using \d+ in your example, which would match something like 33-3333333-333, which is not a date.

If you are worried about a string like 123456-12-1234567 matching when they shouldn't, you can use negative lookarounds:

(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)

See example in action: https://regex101.com/r/nR0hB2/1

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

8 Comments

Well, I do not agree that \d+ matches 33-3333333-333, and I'd mention that \d{4}-\d{2}-\d{2} will find a match even in 122143245-32-321312.
@stribizhev I meant the original example, (\d+)-(\d+)-(\d+) which uses \d+ would match that string. Also, the OP has the date string interleaved with other data and doesn't provide context about what might be around it.
I should have posted a random number instead of my actual input. That would've removed all the confusions. I did not want the date validation else I would have specified it in the question. I just wanted to know how to check if the digits are in that particular format.
I just needed the {4} part . I think I lost 10 points from my rep for something stupid ! :(
@DevonParsons Yes this is exactly what I wanted. Thanks man :)
|

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.