Borrar filtros
Borrar filtros

How to Find the specific character between two repeated strings.

1 visualización (últimos 30 días)
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'

Respuesta aceptada

Stephen23
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
Pratik Yadav
Pratik Yadav el 12 de Dic. de 2019
WOW.........!!! :) I am Just amzed. This is fantastic. Can you do one more favour , Could please eloborate this command regexp(S,'<(\w+)>([^/]*)</\1>','tokens'); So, that I will be able to explore it.
Stephen23
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:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by