MATLAB: Finding first non-digit to left (or right) of a particular character in a string

findstrfindstrings

I have filenames formatted as, for example, follows:
20170629_material_otherinfo_stuff0p12_v1.dat
I would like to pull out the "0p12" portion of the filename to save in a structure. The issues are:
  • It could have multiple digits before the "p" and also more than 2 after the p
  • What's written in "stuff" can change content and length
  • Depending on the test, Sometimes _v* is there and sometimes the filename simply ends with "*0p12.dat"
I can use strfind() to figure out where the "p" is in my string (it's always the last "p"). I'd like to then search for the first location in the string to the left of the p and the first to the right that are not digits. Is this possible?
Thanks for any help!

Best Answer

S = '20170629_material_otherinfo_stuff0p12_v1.dat';
cell2mat( regexp(S, '\d+p\d+', 'match') )