How to Find the specific character between two repeated strings.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have input string as
Input =<NETWORK>CAN</NETWORK> <DESCRIPTION>Despacito</DESCRIPTION> <INITVALUE>0</INITVALUE><FRAME>ADAS_A08SC_FD</FRAME> <DATA>ADAS_HazardLampRequest</DATA>
from this string I want data which is present between two <NETWORK> i.e CAN same for Des I want answer as Despacito and inital value as '0'
Frame as 'ADAS_A08SC_FD' and ans for Data field would be 'ADAS_HazardLampRequest'
0 commentaires
Réponse acceptée
Stephen23
le 12 Déc 2019
Modifié(e) : Stephen23
le 12 Déc 2019
>> V = '<NETWORK>CAN</NETWORK> <DESCRIPTION>Despacito</DESCRIPTION> <INITVALUE>0</INITVALUE><FRAME>ADAS_A08SC_FD</FRAME> <DATA>ADAS_HazardLampRequest</DATA>';
>> C = regexp(V,'<(\w+)>([^/]*)</\1>','tokens');
>> C = vertcat(C{:});
>> S = cell2struct(C(:,2),C(:,1),1)
S =
NETWORK: 'CAN'
DESCRIPTION: 'Despacito'
INITVALUE: '0'
FRAME: 'ADAS_A08SC_FD'
DATA: 'ADAS_HazardLampRequest'
>> S.NETWORK
ans =
CAN
2 commentaires
Stephen23
le 12 Déc 2019
Modifié(e) : Stephen23
le 12 Déc 2019
"...please eloborate this command..."
The function regexp is documented here:
Regular expressions are documented here (note that the documentation includes examples of simple HTML parsing, i.e. matching tags just like you want to do):
The regular expression I used works like this:
< % match literal '<'
( % start token group 1 (for the tag)
\w+ % match 1 or more letters or digits
) % end token group 1
> % match literal '>'
( % start token group 2 (for the data)
[^/]* % match zero or more characters that are not '/'
) % end token group 2
</ % match literal '</'
\1 % match first token group (whatever it may be)
> % match literal '>'
The regexp option 'tokens' returns the tokens specified by the regular expression.
If you want to explore regular expressions further then you might like to download my FEX submission iregexp:
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings 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!