How to take a common particular part of string
3 views (last 30 days)
Show older comments
Mekala balaji
on 28 May 2016
Commented: Image Analyst
on 28 May 2016
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 Comment
the cyclist
on 28 May 2016
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".)
Accepted Answer
Azzi Abdelmalek
on 28 May 2016
s={'PR.VK0K200E5T00_T123'
'PR.VBK0K800E3F00_R243'
'PR.VP124K800E5T00'
'PR.ZVK0K10E5T00_T123_FF99'}
out=regexp(s,'\d+E\d+','match')
out=[out{:}]'
5 Comments
Azzi Abdelmalek
on 28 May 2016
Edited: Azzi Abdelmalek
on 28 May 2016
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')
More Answers (1)
Image Analyst
on 28 May 2016
Edited: Image Analyst
on 28 May 2016
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 Comment
Image Analyst
on 28 May 2016
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.
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!