Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

2 Answers 2

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 at 2:15
@Algohi You're welcome, and thank you. :-) –  Mr.Wizard Jun 6 at 4:08
add comment

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 at 2:16
add comment

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.