How to take a common particular part of string
3 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have the below cell array. Please some one help me how to do this.
PR.VK0K200E5T00_T123
PR.VBK0K800E3F00_R243
PR.VP124K800E5T00
PR.ZVK0K10E5T00_T123_FF99
My output is below.
200E5
800E3
800E5
10E5
Many thanks in advance.
1 件のコメント
the cyclist
2016 年 5 月 28 日
The hard part here is interpreting the exact rule you want to use to parse the input. Azzi's solution below assumes you want the "E", and the numeric characters before and after that. (Seems reasonable, given your input, and that it looks like numbers in exponential notation. However, it will break if you have an input like "PR.ZVK0K10E5T00_T123_FF5E99".)
採用された回答
Azzi Abdelmalek
2016 年 5 月 28 日
s={'PR.VK0K200E5T00_T123'
'PR.VBK0K800E3F00_R243'
'PR.VP124K800E5T00'
'PR.ZVK0K10E5T00_T123_FF99'}
out=regexp(s,'\d+E\d+','match')
out=[out{:}]'
5 件のコメント
Azzi Abdelmalek
2016 年 5 月 28 日
編集済み: Azzi Abdelmalek
2016 年 5 月 28 日
s={'PR.VK0K200E5T00_T123'
'PR.VBK0K800E3F00_R243'
'PR.VP124K800E5T00'
'PR.ZVK0K10E5T00_T123_FF99'
'PR.ZVK0K10E5T00_T123_FF5E99'}
out=regexp(s,'\d+E\d+','match','once')
その他の回答 (1 件)
Image Analyst
2016 年 5 月 28 日
編集済み: Image Analyst
2016 年 5 月 28 日
Here's a non-regexp way that I think should be a lot easier to understand:
% Create data.
s1 = 'PR.VK0K200E5T00_T123'
s2 = 'PR.VBK0K800E3F00_R243'
s3 = 'PR.VP124K800E5T00'
s4 = 'PR.ZVK0K10E5T00_T123_FF99'
% Now find the number
str = s1; % To test, set = s2, s3, and s4
% Find the final E
lastE = find(str == 'E', 1, 'last')
% Find the final K
lastK = find(str(1:lastE-1) == 'K', 1, 'last')
% Extract the string from 1 character after the K
% to one character after the last E.
output = str(lastK+1 : lastE+1)
This works for all the cases that you presented.
1 件のコメント
Image Analyst
2016 年 5 月 28 日
That's why it's important to give all cases in advance. So now I've added a line to handle the case of "PR.ZVK0K10E5T00_T123_FF5E99" which has an extra E in the string:
% Now find the number
str = 'PR.ZVK0K10E5T00_T123_FF5E99';
% Truncate string after first underline
str = str(1:find(str=='_', 1, 'first'));
% Find the final E
lastE = find(str == 'E', 1, 'last')
% Find the final K
lastK = find(str(1:lastE-1) == 'K', 1, 'last')
% Extract the string from 1 character after the K
% to one character after the last E.
output = str(lastK+1 : lastE+1)
Now it gives 10E5. I hope that is what you wanted.
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!