필터 지우기
필터 지우기

How to take a common particular part of string

조회 수: 4 (최근 30일)
Mekala balaji
Mekala balaji 2016년 5월 28일
댓글: Image Analyst 2016년 5월 28일
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
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
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
Mekala balaji
Mekala balaji 2016년 5월 28일
Sir,
Can it be possible to further generalize, like as you said "PR.ZVK0K10E5T00_T123_FF5E99", it will also break, but to generalize it, the left and right side should be numeric (not a Aplhabets, or _ or .(dot))., So that my out put will avoid 5E99.
Many thanks,
Azzi Abdelmalek
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
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
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 CenterFile 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!

Translated by