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 Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by