Given the text:

123456789:aaaa-!-www 123456789:aaaa-Z-www 1234-!-56789:aaaawww 12345-Z-6789:aaaawww 

Task: In edit - we write the characters "!" and "Z" and the program excludes all lines, where after the ":" sign there are "!" and "Z".

The output should be:

 1234-!-56789:aaaawww 12345-Z-6789:aaaawww 

I did it like this: It works, but for some reason, the whole line is taken into account . But what is needed is a search to occur after the - symbol ":" . How to fix it ?

 procedure TForm1.Button1Click(Sender: TObject); const D = [' ', #9, #10, #13]; var fall, f1: TextFile; Da : set of Char; i, Len, LenW, CntA : Integer; S,L, R: String; g:Tstringlist; begin //Открываем диалог выбора файла if OpenDialog1.Execute then begin g:=Tstringlist.Create; //Составляем множетсво букв. В множество добавляем строчный и заглавный //варианты букв. S := Edit1.Text; // В Edit записываем список символов Da := []; for i := 1 to Length(S) do if not (S[i] in D) then Da := Da + [ AnsiUpperCase(S[i])[1], AnsiLowerCase(S[i])[1] ]; if Da = [] then begin ShowMessage('Буквы не заданы. Действие отменено.'); Exit; end; AssignFile(fall, OpenDialog1.FileName); reset(fall); end; //работаем по строкам в файле while not Eof(fall) do begin //читаем строку ReadLn(fall, S); Len := Length(S); LenW := 0; CntA := 0; for i := 1 to Len do begin //Пропускаем разделители. if S[i] in D then Continue; //Подсчёт заданных букв в слове. if S[i] in Da then Inc(CntA); //Уточняем длину слова. Inc(LenW); //Отслеживаем конец слова. if (i = Len) or (S[i + 1] in D) then begin //Если заданных букв нет в слове - распечатываем его. if CntA = 0 then // Записываем рзультат g.Add( Copy(S, i - LenW + 1, LenW) ); //Сброс счётчика заданных букв. CntA := 0; //Сброс длины слова. LenW := 0; end; end; end; g.SaveToFile('Результат.txt'); g.Free; CloseFile(fall); end; 
  • one
    Judging by the example - the condition should be "symbols"! " Or " Z "."? And where is the colon involved in the code? - MBo
  • @MBo, Exactly here (at this moment I got stuck) I can’t guess where it is to prescribe a check with a colon, so that the search is performed after the colon. - Tatiana

1 answer 1

 bSemicolon := False; for i := 1 to Length(S) do case S[i] of ':' : bSemicolon := True; '!','Z': if bSemiColon then что-то делать, можно остановить цикл Break; else ... //что-то делать для других символов end; 

In addition, you can use Pos with the job starting position

 BadChars := '!Z'; sPos := Pos(":", s); if sPos > 0 then for k = 1 to Length(BadChars) do if (Pos(BadChars[k], s, sPos + 1) > 0) then s плохая 
  • one
    With Pos (':') + 1. You have been shown such examples many times. - MBo
  • one
    What happened with which approach? - MBo
  • one
    Pos code added - MBo
  • one
    You can loop pos with all characters. - MBo
  • one
    Correct, but irrational. Added by. - MBo