필터 지우기
필터 지우기

Searching for a particular substring in a Text file

조회 수: 1 (최근 30일)
Saeed Soltani
Saeed Soltani 2016년 5월 27일
댓글: Saeed Soltani 2016년 5월 27일
Hi everyone, I am searching for a particular part of my text file. I used textscan but the problem is I don’t know formatSpec well. My text file context looks like this:
/SoftwareComponents/DistMgr /SoftwareComponents/Dummy
I would like to get child names of SoftwareComponents, Actually the output must be:
ans{1}= DistMgr
ans{2}= Dummy
It is my pleasure if someone can help. Thank you

채택된 답변

Guillaume
Guillaume 2016년 5월 27일
편집: Guillaume 2016년 5월 27일
It's not exactly clear what the actual format of your text file is. Are all entries just separated by one space?
Anyway, if the file consists just of a series of text strings, I wouldn't use textscan for that. I would simply read the whole file in one go, then use a regular expression to get the result:
filecontent = fileread('c:\somewhere\somefile.txt');
scchildren = regexp(filecontent, '(?<=/SoftwareComponents/)\w+', 'match')
Note that the above regular expression assumes that the name of a child consists only of characters from the set [A-Za-z_0-9].
  댓글 수: 3
Guillaume
Guillaume 2016년 5월 27일
편집: Guillaume 2016년 5월 27일
Do'h! I forgot to pass filecontent to regexp. Fixed now, sorry about that.
Saeed Soltani
Saeed Soltani 2016년 5월 27일
Right, Thank you man.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by