find a string using regular expression using a special condition

1 次查看(过去 30 天)
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