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!