How to Find the specific character between two repeated strings.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pratik Yadav
el 12 de Dic. de 2019
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 comentarios
Respuesta aceptada
Stephen23
el 12 de Dic. de 2019
Editada: Stephen23
el 12 de Dic. de 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 comentarios
Stephen23
el 12 de Dic. de 2019
Editada: Stephen23
el 12 de Dic. de 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:
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!