Converting rough strings to exact strings

2 ビュー (過去 30 日間)
Gabriel Stanley
Gabriel Stanley 2022 年 9 月 7 日
コメント済み: Gabriel Stanley 2022 年 9 月 8 日
I have a string array of filenames which are names in an semi-consistent manner, e.g.:
AllFiles
AllFiles =
4x1 string array
"textIdontCareAbout_Phenolic32_Group5_textIdontCareAbout"
"textIdontCareAbout_P1_textIdontCareAbout"
"textIdontCareAbout_Epx2_G3_textIdontCareAbout"
"textIdontCareAbout_Epoxy_105_textIdontCareAbout"
Im trying to figure out how to extract & convert the inconsistent substrings of interest (the stuff between "textIdontCareAbout") into a consistent format, e.g.:
AllFiles
AllFiles =
4x1 string array
"P32G5"
"P1"
"E2G3"
"E105"
I had been avoiding using regexp, but having caved and decided to work with that, I'm trying to figure out an elegant way to do this conversion. At present the only thing I can see working is manually checking for each possible phrasing style I see when manualy searching through the data I have at present.
Is there a better way to go about this, or even just some suggestions to how to define the regexp in a way to have as few searches as possible?
  4 件のコメント
Gabriel Stanley
Gabriel Stanley 2022 年 9 月 8 日
...I feel bad, because in the time you've developed this solution I also managed to develop the appropriate regexp for the Phenolic and Epoxy groups, and am now going figuring our how to fuse the two resulting arrays.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2022 年 9 月 8 日
S = [...
"textIdontCareAbout_Phenolic32_Group5_textIdontCareAbout"
"textIdontCareAbout_P1_textIdontCareAbout"
"textIdontCareAbout_Epx2_G3_textIdontCareAbout"
"textIdontCareAbout_Epoxy_105_textIdontCareAbout"];
T = regexp(S,'_.+_','match','once');
T = regexprep(T,'[^A-Z\d]','')
T = 4×1 string array
"P32G5" "P1" "E2G3" "E105"
  3 件のコメント
Gabriel Stanley
Gabriel Stanley 2022 年 9 月 8 日
The cell brackets {} are because I forgot I don't need them for string arrays. And I've taken your direction and added the 'once' option to the regexpi call. Thank you for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Find more on Characters and Strings in Help Center and File Exchange

タグ

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by