Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the large number of files in which the file's names follow certain pattern. for example here are names of two files

name1="event_No. 2_20140311_910_914.jpg"
name2="event_No. 403_20140311_230727_230731.jpg"

I need to extract the last two numbers of the names to get something like this (String or Expression, it does not matter):


I tried StringTake but it is not supporting Rules and patterns.

can any one suggest some way to do this?


share|improve this question
up vote 3 down vote accepted
StringCases[{name1, name2}, DigitCharacter ..][[All, -2 ;;]]
{{"910", "914"}, {"230727", "230731"}}

For fun or interest, also:

With[{SR = StringReverse},
  StringCases[{name1, name2} // SR, DigitCharacter .., 2] // SR
{{"914", "910"}, {"230731", "230727"}}
share|improve this answer
Thanks a lot Mr.Wizard. I am impressed with your knowledge of Mathematica. – Algohi Jun 6 '14 at 2:15
@Algohi You're welcome, and thank you. :-) – Mr.Wizard Jun 6 '14 at 4:08

It is really straight forward when you use StringCases.

StringCases[{name1, name2}, 
 start__ ~~ "_" ~~ s1 : NumberString ~~ "_" ~~ s2 : NumberString ~~ ".jpg" :> {s1, s2}]

(* {{{"910", "914"}}, {{"230727", "230731"}}} *)
share|improve this answer
Thanks a lot halirutan. – Algohi Jun 6 '14 at 2:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.