find a string using regular expression using a special condition

1 visualización (últimos 30 días)
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 ------------ ------------ ------------ ------------

Respuesta aceptada

per isakson
per isakson el 9 de Nov. de 2019
Editada: per isakson el 9 de Nov. de 2019
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.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by