How to take a common particular part of string
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mekala balaji
le 28 Mai 2016
Commenté : Image Analyst
le 28 Mai 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 commentaire
the cyclist
le 28 Mai 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".)
Réponse acceptée
Azzi Abdelmalek
le 28 Mai 2016
s={'PR.VK0K200E5T00_T123'
'PR.VBK0K800E3F00_R243'
'PR.VP124K800E5T00'
'PR.ZVK0K10E5T00_T123_FF99'}
out=regexp(s,'\d+E\d+','match')
out=[out{:}]'
5 commentaires
Azzi Abdelmalek
le 28 Mai 2016
Modifié(e) : Azzi Abdelmalek
le 28 Mai 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')
Plus de réponses (1)
Image Analyst
le 28 Mai 2016
Modifié(e) : Image Analyst
le 28 Mai 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 commentaire
Image Analyst
le 28 Mai 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.
Voir également
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!