-1

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.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
v1shnu
  • 2,211
  • 8
  • 39
  • 68
  • 1
    Post your attempts to achieve this. – takendarkk Sep 02 '15 at 11:59
  • 1
    What is your question? What language are you using? Please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask). – Jason Cust Sep 02 '15 at 12:00
  • How is this question unclear to anyone ?? I'm asking for the regex pattern to match this. – v1shnu Sep 02 '15 at 12:07
  • 1
    The regex you have is good to go. Just check with [`date_parse`](http://php.net/manual/en/function.date-parse.php) 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. – Wiktor Stribiżew Sep 02 '15 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. – Devon Parsons Sep 02 '15 at 12:11
  • Hi there. I'm not a downvoter, nor a closevoter, however: what's unclear to me is whether you just want to check if there are some digits in the format dddd-dd-dd, or if you want to check whether there is a valid date. Given `att_token_1234-99-99`, what should the output be? – Andrea Corbellini Sep 02 '15 at 12:17
  • 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. – Wiktor Stribiżew Sep 02 '15 at 12:18
  • @AndreaCorbellini That isn't my requirement else I would have specified in my question as to I need it to be validated as a date. I just wanted the digits. – v1shnu Sep 02 '15 at 12:30
  • @stribizhev Aren't regexes not dependent on the language used ? – v1shnu Sep 02 '15 at 12:31
  • They are, that is why it is important to indicate the language as a question tag. – Wiktor Stribiżew Sep 02 '15 at 12:32
  • 1
    I'm voting to close this question as off-topic because no programming language is indicated while it is a requirement for regex questions. – Wiktor Stribiżew Sep 02 '15 at 12:43
  • @stribizhev I did NOT want the date validation, else I would have mentioned it in my question. My question stated that I want to see if the string contains the digits in that particular format for which I have received an answer. I was being asked questions irrelevant to my actual question for no reason. – v1shnu Sep 02 '15 at 13:09
  • The only thing I missed was the language since I thought regex work irrespective of language. Even then 5 down votes and 3 close requests are a little too harsh I would say. – v1shnu Sep 02 '15 at 13:10
  • 1
    I relived mine, but I think you will agree you did not put enough effort into it. Even if you need a regex for that: there are tons of them here and all over the place. Just type `[regex] date`, and you are bound to find some. [Here](http://stackoverflow.com/questions/30123524/date-format-comparmission/30123756#30123756) [are](http://stackoverflow.com/a/29233721/3832970) some of my [answers](http://stackoverflow.com/a/30778800/3832970). – Wiktor Stribiżew Sep 02 '15 at 13:17
  • @stribizhev It is still NOT about the date. The title of my question is 'Check if number is present and in particular pattern.' Anyway thanks :) – v1shnu Sep 02 '15 at 13:21
  • 1
    Yes, posting a clear question with your own efforts, comprehensive examples and expected output is key. – Wiktor Stribiżew Sep 02 '15 at 13:45

1 Answers1

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

Devon Parsons
  • 1,234
  • 14
  • 23
  • 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`. – Wiktor Stribiżew Sep 02 '15 at 12:17
  • 1
    @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. – Devon Parsons Sep 02 '15 at 12:34
  • 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. – v1shnu Sep 02 '15 at 13:05
  • I just needed the {4} part . I think I lost 10 points from my rep for something stupid ! :( – v1shnu Sep 02 '15 at 13:06
  • @DevonParsons will this match exactly 4,2 and 2 digits or less than or more than the digits specified ? – v1shnu Sep 02 '15 at 13:06
  • @ViChU: Now, yes. `(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)` contains the look-arounds that will ensure the match only if the string is not enclosed with digits. – Wiktor Stribiżew Sep 02 '15 at 13:47
  • @ViChU It'll match exactly 4 or 2 digits, but if those digits are at the very start/end of the match and there are MORE digits before/after the match, they will be ignored when you might have wanted them to prevent the match from being found. See the updated answer. – Devon Parsons Sep 02 '15 at 13:47
  • 1
    @DevonParsons Yes this is exactly what I wanted. Thanks man :) – v1shnu Sep 03 '15 at 05:15