Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function StrMatches(Expr, Str: string; Ignore: Char; Sensitive: Boolean; var Match: String): Boolean;
- var
- TSA: TStrArray;
- h, i, _right, p, _left, s, l, t: Integer;
- b, e: Boolean;
- begin
- Match := '';
- Result := False;
- _right := Length(Expr);
- l := Length(Str);
- if ((_right > 0) and not (_right > l)) then
- begin
- TSA := Explode(Ignore, Expr);
- h := High(TSA);
- s := 0;
- for i := 0 to h do
- if (Length(TSA[i]) > 0) then
- begin
- TSA[s] := TSA[i];
- s := (s + 1);
- end;
- if (s > 0) then
- begin
- SetLength(TSA, s);
- h := High(TSA);
- b := (Expr[1] = Ignore);
- e := (Expr[_right] = Ignore);
- t := Length(TSA[0]);
- case (h = 0) of
- False:
- begin
- case b of
- True:
- begin
- _left := 1;
- _right := _left;
- s := 0;
- end;
- False:
- begin
- _left := Pos(TSA[0], Str);
- _right := (_left + t);
- s := 1;
- end;
- end;
- if ((h > (s - 1)) and (_left > 0)) then
- begin
- case Sensitive of
- True:
- for i := s to h do
- begin
- p := PosEx(TSA[i], Str, _right);
- if (p > 0) then
- _right := (p + Length(TSA[i]))
- else
- Exit;
- end;
- False:
- for i := s to h do
- begin
- p := LastPos(TSA[i], Str);
- if (p > _right) then
- _right := (p + Length(TSA[i]))
- else
- Exit;
- end;
- end;
- case (b and e) of
- False:
- if e then
- Match := Copy(Str, _left, ((l - _left) + 1))
- else
- Match := Copy(Str, _left, (_right - _left));
- True: Match := Copy(Str, 1, l);
- end;
- Result := True;
- end;
- end;
- True:
- begin
- p := Pos(TSA[0], Str);
- Result := (p > 0);
- if Result then
- case b of
- True:
- case e of
- False: Match := Copy(Str, 1, ((t + p) - 1));
- True: Match := Copy(Str, 1, l);
- end;
- False:
- case e of
- False: Match := Copy(Str, p, t);
- True: Match := Copy(Str, p, ((l - p) + t));
- end;
- end;
- end;
- end;
- end;
- end;
- end;
- var
- Str, expression, Match: string;
- begin
- ClearDebug;
- expression := 'This is text';
- Str := 'This is text yyyyyyyrt';
- if StrMatches(expression, Str, '*', True, Match) then
- WriteLn(Match);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement