Take the 2-minute tour ×

I need a regex for this format xxxx-xxx-xx.jpg where x is digits [0-9].

To match for example: 3402-560-27.jpg

share|improve this question

closed as too localized by Charles, Lev Levitsky, code_burgar, jtbandes, Rafał Rawicki Dec 29 '12 at 19:44


Give it a try yourself. You can do it! –  GregS Dec 28 '12 at 13:31
This is not a "do your work for you" site. we like to see that some effort has been put into answering it yourself. Have you done any reading on regex? This is simple enough that it probably wouldn't take long to answer it yourself... –  Chris Dec 28 '12 at 13:31
I know guys, and thats why I am embarrassed about asking for it. I'm a student and of course I've read about it, but I couldn't get the hang of it.. Though, after seeing the answer it became pretty clear. I guess I made it harder than it was. Once again, I'm sorry guys –  aLoveForCoffee Dec 28 '12 at 13:33
gskinner.com/RegExr -_- –  Leri Dec 28 '12 at 13:33
The guys are being cranky this morning. It's hard to know where to start when you first approach regex. Now you have an idea of how it works so you can get a little further along next time before you come here to ask. –  DOK Dec 28 '12 at 13:37

2 Answers 2

up vote 6 down vote accepted

This regex is what you want:


\d represents digits so \d{4} mean 4 digits.. in regex a . matches any single character so to match a literal . it needs to be escaped with a \.

share|improve this answer

This is one of the simplest regexes to write: put \d for each digit, - for each dash, and a \. for each dot. Letters correspond to themselves, so jpg goes in unchanged.

When you have more time, you can earn some "points for style" by learning about the explicit quantifier notation for repeated groups.

share|improve this answer
