2
$\begingroup$

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):

{"910","914"}
{"230727","230731"}

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

can any one suggest some way to do this?

Thanks

$\endgroup$
3
$\begingroup$
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"}}
$\endgroup$
  • $\begingroup$ Thanks a lot Mr.Wizard. I am impressed with your knowledge of Mathematica. $\endgroup$ – Algohi Jun 6 '14 at 2:15
  • $\begingroup$ @Algohi You're welcome, and thank you. :-) $\endgroup$ – Mr.Wizard Jun 6 '14 at 4:08
1
$\begingroup$

It is really straight forward when you use StringCases.

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

(* {{{"910", "914"}}, {{"230727", "230731"}}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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