get unknow Strings(Text) out of other strings ?

조회 수: 2 (최근 30일)
Max Müller
Max Müller 2014년 9월 10일
댓글: Star Strider 2014년 9월 11일
Hey Guys, can u give me an example, that shows how to get information out of a long String ? If......
the Word before the Information u want is known. Example
name: Benny age:23 ( I want the Info Age)
Out of this code
<HTML><FONT color="0000FF">Used Amplification(Hidden)</FONT></HTML>
I want the Information:Used Amplification(Hidden)
I guess the KEy is regexp again.....but i think i am wrong....sorry for these stupid questions.

채택된 답변

Guillaume
Guillaume 2014년 9월 10일
If you don't want to learn the regex syntax, you can use strfind:
before = '<HTML><FONT color="0000FF">';
after = '</FONT></HTML>';
start = strfind(str, before) + length(before); %or just length(before)+1 if str always starts with before.
end = strfind(str, after) - 1;
result = str(start:end); %assumes there's only ever one match
It's of course a lot more flexible and shorter with regexes:
result = regexp(str, '<HTML><FONT color="[0-9A-F]+">(.*?)</FONT></HTML>', 'tokens', 'once'); %with added bonus it will work with any color, not just 0000FF.
  댓글 수: 5
Guillaume
Guillaume 2014년 9월 11일
Matlab regular expression engine is slightly different than C++ std::regex and other posix compliant regexes, it's not as good on some things (e.g. captures) but the basics are the same so, yes, you can use tutorial for C++ or any other language.
This one seems to cover the basics and is language agnostic.
Star Strider
Star Strider 2014년 9월 11일
@Guillaume — That has to be one of the best explanations of regexp I’ve read!
+1

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by