find a string using regular expression using a special condition

조회 수: 1 (최근 30일)
RR RR
RR RR 2019년 11월 8일
편집: per isakson 2019년 11월 9일
Hello All,
How to find the line number for the string "liquid, X" in a text file, if and only if the line immidiately before its occarance contains the string "Com(6 ,C4 )"
Molar Distributions Com(5 ,C3 ) Com(6 ,C4 ) Com(7 ,C5 ) Com(8 ,C6 )
Liquid, X Inserted ------------ ------------ ------------ ------------

채택된 답변

per isakson
per isakson 2019년 11월 9일
편집: per isakson 2019년 11월 9일
Assumptions regarding the search terms
  • case is significant. ( "liquid, X" the lower case "l" is a typo.)
  • space is significant
Try
%%
chr = fileread( 'RR.txt' );
cac = strsplit( chr, '\r?\n', 'DelimiterType', 'RegularExpression' );
%%
is_Com6C4 = contains( cac, 'Com(6 ,C4 )' );
is_Liquid = startsWith( cac, 'Liquid, X' );
%%
ix_searched = find( [ false, is_Com6C4 ] & [ is_Liquid, false ] );
where RR.txt contains
Her old collecting she considered discovered. So at parties he warrant oh staying.
Square new horses and put better end. Sincerity collected happiness do is contented.
Liquid, X Inserted ------------ ------------ ------------ ------------
Molar Distributions Com(5 ,C3 ) Com(6 ,C4 ) Com(7 ,C5 ) Com(8 ,C6 )
Sigh ever way now many. Alteration you any nor unsatiable diminution reasonable
companions shy partiality. Leaf by left deal mile oh if easy. Added woman first get led
joy not early jokes.
Molar Distributions Com(5 ,C3 ) Com(6 ,C4 ) Com(7 ,C5 ) Com(8 ,C6 )
Liquid, X Inserted ------------ ------------ ------------ ------------
Not far stuff she think the jokes. Going as by do known noise he wrote round leave.
Warmly put branch people narrow see. Winding its waiting yet parlors married own
Molar Distributions Com(5 ,C3 ) Com(6 ,C4 ) Com(7 ,C5 ) Com(8 ,C6 )
feeling. Marry fruit do spite jokes an times. Whether at it unknown warrant herself
Liquid, X Inserted ------------ ------------ ------------ ------------
winding if. Him same none name sake had post love. An busy feel form hand am up help.
Parties it brother amongst an fortune of. Twenty behind wicket why age now itself ten
If the assumptions are not fullfilled, regular expressions might be needed.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by