Azzera filtri
Azzera filtri

How to Find the specific character between two repeated strings.

1 visualizzazione (ultimi 30 giorni)
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'

Risposta accettata

Stephen23
Stephen23 il 12 Dic 2019
Modificato: Stephen23 il 12 Dic 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 Commenti
Pratik Yadav
Pratik Yadav il 12 Dic 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 il 12 Dic 2019
Modificato: Stephen23 il 12 Dic 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:

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by