How to Find the specific character between two repeated strings.

조회 수: 1 (최근 30일)
Pratik Yadav
Pratik Yadav 2019년 12월 12일
편집: Stephen23 2019년 12월 12일
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'

채택된 답변

Stephen23
Stephen23 2019년 12월 12일
편집: Stephen23 2019년 12월 12일
>> 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
Pratik Yadav
Pratik Yadav 2019년 12월 12일
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 2019년 12월 12일
편집: Stephen23 2019년 12월 12일
"...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:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by